Manually Logging a User In

Problem

You want to manually log a user in.

Solution

Use the Auth::login() method.

This logs the user into the current session, bypassing any authentication.

$user = User::find($user_id);
Auth::login($user);

You can also specify true as the second parameter to set the "remember me" cookie.

Auth::login($user, true);

Discussion

The Auth::attempt() method does this automatically.

In face, if the authentication is successful, Auth::attempt() will call Auth::login() internally.

But, if you need to bypass authentication altogether, this is a great method to use. It's especially useful when testing: just create a temporary route to log a user in using this method.

This will fire the auth.login event.

comments powered by Disqus