Manually Logging a User by ID

Problem

You want to manually log a user in from their ID.

Solution

Use the Auth::loginUsingId() method.

$user = Auth::loginUsingId($user_id);
if ( ! $user)
{
    throw new Exception('Error logging in');
}

This will lookup the user from the $user_id, log them into the session, and return the user object. If null is returned, then you know the $user_id was invalid.

If you want the "remember me" cookie to be set, pass true as the second argument.

Auth::loginUsingId($user_id, true);

Discussion

This does basically the same thing as Auth::login().

In fact, after this method looks up the user it calls Auth::login(). See Manually Logging a User In for more details.

comments powered by Disqus