Attempting to Authenticate with Credentials

Problem

You want to log a user on with code.

Instead of the normal way using filters, you need to validate a user's credentials and optionally log them in.

Solution

Use the Auth::attempt() method.

You must provide the credentials.

$success = Auth::attempt(['username' => 'me', 'password' => 'pass']);

This will attempt to log the user on using username = 'me' and 'password' == Hash::make('pass'). If successful, true is returned and the user is logged in.

The second argument specifies whether or not to set the "remember me" cookie. The default value of this second argument is false.

$success = Auth::attempt($credentials, true);

Now, if $success return true, the remember me cookie will be set.

If you only want to validate and not log the user in, you can pass a third option. The default for this third option is true.

$success = Auth::attempt($credentials, false, false);

Discussion

Nothing to discuss.

comments powered by Disqus