Ad

Laravel 5.2 - User Is Logged In Only After Successfull Login Redirect, Once I Do Action User Is Not More Logged In

- 1 answer

I am using Laravel 5.2 and Laravel Authentication out of the box. Here is my steps: 1) installing laravel 2) php artisan migrate 3) php artisan make:auth

Everything is ready, at least it should be...

Than I open localhost/project/public

a) Press on Register link, registering new account, if successful it redirects to /home and tell that I am logged in and shows my username. b) But once I am clicked to another link (e.g. Home button which redirects to /), than user is no more logged in. And because I am not logged in I am able to see Login and Register links, but they are redirected to /home as it expected.

And I mentioned that if I comment this line in AuthController (to allow access to login page even if I am logged in)

public function __construct()
{
    //$this->middleware('guest', ['except' => 'logout']);
}

after step b) with this commented line I press Login or Register link and... user is logged in again.. Very strange behavior. Same behavior if I open http://localhost/project/public/home, it says that I am logged in.

Generated code from routes.php file:

Route::group(['middleware' => 'web'], function () {
Route::auth();

Route::get('/home', '[email protected]'); });

Does anyone know how to solve this issue?

Ad

Answer

So essentially, this is what my routes.php file looks like and it works perfectly:

<?php

/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/



/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/

Route::group(['middleware' => ['web']], function () {
    //
});

Route::group(['middleware' => 'web'], function () {
    Route::auth();

    Route::get('/', function () {
        return view('welcome');
    });

    Route::get('/home', '[email protected]');
});
Ad
source: stackoverflow.com
Ad