When you use basic auth filter in Laravel 4.2, I noticed that you can't set the realm
(maybe just me?) for the authentication, and therefore when the auth window appears, it looks like this in internet explorer:
Take a look at the official documentation example : http://php.net/manual/en/features.http-auth.php#example-372
If I had used this, the above login window will say Restricted area
rather than null
.
Any idea how to set the realm of basic auth in Laravel?
Also, how or where do you set/style the text to display when the auth fails or user hits the cancel button?
Found that, but nothing laravelish.
header('WWW-Authenticate: Basic realm="REALM"');
header('HTTP/1.0 401 Unauthorized');
Use the following route filter :
Route::filter('auth.basic', function() {
$response = Auth::basic();
if (!is_null($response)) {
return $response->header("WWW-Authenticate", 'Basic realm="REALM"');
}
});
Adapted from this answer which explains how it works in detail; basically Auth::basic()
returns either null
if the user is already authenticated or a 401 Unauthorized
response with a WWW-Authenticate
header, if the response isn't null we call the header
method on that Response
to replace that header with our custom version of WWWi-Authenticate
including the realm
parameter; if you look at that method you'll notice that it's set to replace any previous headers by default.
Finally the return value of that method is the response object itself so we can return that instead of explicitly returning $response
on a new line.