How to show error message when an user access an url that are protected by Laravel middleware auth (['middleware'=>'auth])?
1 Answer
Open app/Http/Middleware/Authenticate.php and inside the handle() method change the condition to somethig like this:
if (Auth::guard($guard)->guest()) {
return redirect()->guest('login')->with(['message' => 'You should login.']);
}
So then you'll have the variable $message in your view, you just have to output it.
2 Comments
JCarlosR
From L5.4 the Authenticate class is now a part of the Laravel core:
\Illuminate\Auth\Middleware\Authenticate. How to modify it?JCarlosR
It is possible via the exceptions handler: laracasts.com/discuss/channels/general-discussion/…