laravel redirect if logged in

后端 未结 5 1802
孤独总比滥情好
孤独总比滥情好 2021-01-04 00:35

i am using laravel 5.1.8. i am making a login/registration system. i made a controller named AdminController and protect it with middleware.

but i am using laravel\'

相关标签:
5条回答
  • 2021-01-04 01:19

    go to App\Http\Middleware\RedirectIfAuthenticated then change it from

    public function handle($request, Closure $next)
    {
        if ($this->auth->check()) {
            return redirect('/home');
        }
    
        return $next($request);
    }
    

    to

    public function handle($request, Closure $next)
    {
        if ($this->auth->check()) {
            return redirect('/admin');
        }
    
        return $next($request);
    }
    
    0 讨论(0)
  • 2021-01-04 01:24

    when a user is successfully authenticated, they will be redirected to the /home URI, which you will need to register a route to handle. You can customize the post-authentication redirect location by defining a redirectPath property on the AuthController:

    protected $redirectPath = '/dashboard';

    0 讨论(0)
  • 2021-01-04 01:29

    Include \App\Http\Middleware\RedirectIfAuthenticated::class middleware in $middlewareGroups "web" array after \Illuminate\Session\Middleware\StartSession::class middleware

    then modify your redirect path in handle() method of RedirectIfAuthenticated

    public function handle($request, Closure $next, $guard = null)
        {
            //check if authenticate && second is condition when we need to redirect i.e, 
             if(Auth::guard($guard)->check() && $request->route()->named('login') ) {
            return redirect()->route('dashboard');
        }
    
            return $next($request);
        }
    
    0 讨论(0)
  • 2021-01-04 01:30

    You can check using auth function.

    public function checkLogin()
    {
    
        if (auth()->user()) 
        {
             return redirect(route('home'));
        }
        else
        {
               return redirect(route('login'));
        }
    }
    
    0 讨论(0)
  • 2021-01-04 01:32

    Add this to your AuthController:

    protected $redirectTo = '/admin';
    

    This tells all the redirect methods in the various traits to redirect there instead of to /home.

    0 讨论(0)
提交回复
热议问题