0

How to show error message when an user access an url that are protected by Laravel middleware auth (['middleware'=>'auth])?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

From L5.4 the Authenticate class is now a part of the Laravel core: \Illuminate\Auth\Middleware\Authenticate. How to modify it?
It is possible via the exceptions handler: laracasts.com/discuss/channels/general-discussion/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.