Léo Coco
Léo Coco

Reputation: 4282

Laravel authentication not persistent

I'm trying to make alive again an old project using Laravel 5.4 and Laravel Valet.

I'm facing an issue with authentication.

LoginController

public function authenticated(Request $request, User $user){
        $previous_session = $user->session_id;
        if ($previous_session) {
            Session::getHandler()->destroy($previous_session);
        }
        $user = Auth::user();
        $user->session_id = Session::getId();
        $user->save();
        Auth::login($user, true);
        return redirect('testlogin');
    }

Routes

Route::get('testlogin', function () {
    dd(\Illuminate\Support\Facades\Auth::check());
});

In the LoginController, $user is retrieved and not null, but as soon as a redirection is made Auth::check() is false

Questions

What's wrong ? I cannot make make up my mind on this

Upvotes: 0

Views: 214

Answers (2)

Léo Coco
Léo Coco

Reputation: 4282

In the .env file, the variable SESSION_DOMAIN was not set properly and did not reflect the local domain (which had changed between now and few years ago)... Stupid error but I hope it might prevent others to do the same.

Upvotes: 0

Watercayman
Watercayman

Reputation: 8168

This drove me crazy a few times as well. Most likely your 'testLogin' route is not contained within the auth middleware, which would not give you auth at the time of the route passing. If this is the case, move your 'testLogin' route inside the following:

Route::group(['middleware' => ['auth']], function () { ... HERE ... }

Upvotes: 1

Related Questions