Skip to content
Advertisement

multiple authentications on laravel

I have built in laravel an authentication system on user table. I have to make two login pages for admin and normal user on same users table. I have use spatie roles and permission but it does not stop admin login from normal user login page and vice versa.

I have add checkmaster middleware just for accepting my question on stackoverflow.

namespace AppHttpMiddleware;

use Closure;

class CheckMaster
{
    /**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Auth::user()->hasRole('master')) {
            return $next($request);
        }
        return redirect('home');
        return $next($request);
    }
}

Advertisement

Answer

The middleware is working only when user try to go in certain route.

Therefore, the middleware CheckMaster only takes place when logon user tried to go in certain route.

If you are implementing with AuthLoginController.php, you can override the function authenticated when user is authenticated, example code:

protected function authenticated(Request $request, $user)
{
    if ($user->hasRole('master')) {
        return redirect('master-home');
    }

    return redirect('home');
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement