Attempting to Authenticate Using Basic Auth

Problem

You want to authenticate the user using HTTP basic authentication.

Solution

Use the Auth::basic() method.

You can use it without any arguments and it will try matching the HTTP auth user to the email.

$result = Auth::basic();
if ($result)
{
    throw new Exception('invalid credentials');
}

If the user field is something other than 'email' you can specify it with the first argument.

$result = Auth::basic('username');
if ($result)
{
    throw new Exception('invalid credentials');
}

You can even pass the actual request you want to use. Normally, it uses the current request.

$result = Auth::basic('email', $request);

Regardless of the method you use, the method returns a Response which you can pass back to the user for a 401 Invalid Credentials error.

Discussion

The validation will fire an auth.attempt event with the credentials.

If the authentication is successful then a auth.login event will fire.

The Auth::basic() method is used by the default auth.basic filter Laravel provides.

comments powered by Disqus