Search code examples
authenticationlaravel-5.3

Laravel 5.3 manual authentication


Authenticate user by link (Laravel 5.3)

I'm trying to authenticate user when he follows a special link. I find the user by the link parameters and authenticate him like this

Auth::loginUsingId($client->id);

After that Auth::user() returns the user I needed, that's all fine.

But when I trying to acccess user's profile page it redirects me to /login.

If I log in in browser using the same user's credentials I can see the profile page. Seems that it doesn't save info to session.

What have I missed?


Solution

  • I have seen that if you output anything before Auth::attempt() (same as loginUsingId) it does not work. Make sure you have no echo statements, dd, print, or anything else before or after you attempt the login. But, this should work for you:

    Say, for this example that your URL is somedomain.com/autoLogin?userid=1

     public function autoLogin(Request $request){
    
        $id = $request->userid;
    
        $user = Account::find($id);
        Auth::login($user);
    
     } 
    

    This will persist the session.