0
votes

I have this admin middleware:

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::check()) {
        if ($request->user()->is_admin == 1) {
            return $next($request);
        }

        return redirect('/login');
    } else {
        return redirect('/login');
    }
}

And in the logincontroller, if the user is admin, they are redirected to /admin. and if not redirected to /home.

protected function authenticated()
{
    if (auth()->user()->is_admin == 1) {
        return redirect('/admin');
    } else {
        return redirect('/home');
    }
}

Now, when admin logs in, they are redirected to /admin but, on clicking back button of browser they are in /home. How could i not redirect the admins to /home. /home is under auth middleware group.

2

2 Answers

1
votes

The reason for this is because that user is now authenticated.

Laravel comes with a Middleware called RedirectIfAuthenticated which will check if the user is authentitcated and if so redirect them somewhere else.

You would need to edit that middleware to be something like:

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        return $request->user($guard)->is_admin
            ? redirect('/admin')
            : redirect('/home');
    }

    return $next($request);
}

Hope this helps!

0
votes

That is because you are authenticated. When you press back button it means from /admin you want to go back to /login but before calling view it is checked where the user is logged in or not. Since you haven't logged out and are logged in, you will be redirected to default path /home ..

In Auth folder there is a LoginController where you can see this line

protected $redirectTo = '/home';

If you want to add logic you can change it here. This is the default path.