4

I am trying to authenticate user against an API in Laravel 5.3. It is not working. Auth does not persist once the user redirects after login.


Routes are configured like:

Route::group(['middleware' => 'web'], function () {
    Route::group(['middleware' => 'auth'], function () {
        Route::get('/', 'Users\DashboardController@index');
    });
    // Authentication routes...
    Route::get('login', 'Auth\LoginController@getLogin');
    Route::post('login', 'Auth\LoginController@postLogin');
    Route::get('logout', 'Auth\LoginController@getLogout');

});

In LoginController.php I'm using

protected $redirectTo = '/';
public function getLogin()
{
    return view('auth.login');
}

public function postLogin(Request $request)
{
    $this->login($request);
}

After login which is successfully saved in file, the session is not started for route '/'.

So on 'login' route:

dd($session); in Illuminate\Auth\SessionGuard returns #started : true

Store {#153 ▼
 #id: "fo9Q8WuBio5BBus70WmzFPZmkXkjsYQlYEHsVHI4"
 #name: "laravel_session"
 #attributes: array:4 [▼]
 #bags: []
 #metaBag: MetadataBag {#144 ▼}
 #bagData: array:1 [▼]
 #handler: FileSessionHandler {#154 ▼}
 #started: true

}

but #started is false when the route is in 'auth' middleware group ('/' in my case).

I checked some and #started is also false when I put the '/' route outside 'auth' middleware group but calling Auth::check() in controller.

This problem seems to have existed in 5.2 (Auth not persisting in Laravel 5.2 ), but the solution there not working for me.

Please help me understand why I can't access the '/' route after logging in. Thank you!

1 Answer 1

1

change kernel.php 'api'

api' => [
            'throttle:60,1',
            'bindings',
        ],

to

'api' => [
            'throttle:60,1',
            'bindings',
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Session\Middleware\StartSession::class,
        ],

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.