Laravel 5 - After login redirect back to previous page

前端 未结 13 2334
广开言路
广开言路 2020-11-30 04:41

I have a page with a some content on it and a comments section. Comments can only be left by users who are signed in so I have added a login form to the page for users to si

相关标签:
13条回答
  • 2020-11-30 05:30

    The Laravel 5.6, When user insert wrong credentials then login page will reload and session(['link' => url()->previous()]); will take login URL in link variable. So the user will redirect to a login page again or redirect to /home if login success. So to avoid these below code working for me! After that no matter how much time user insert wrong credentials he will redirect after login to exactly where he was before login page.

    Update or overwrite public function showLoginForm() in LoginController.

    public function showLoginForm()
    {
        if (session('link')) {
            $myPath     = session('link');
            $loginPath  = url('/login');
            $previous   = url()->previous();
    
            if ($previous = $loginPath) {
                session(['link' => $myPath]);
            }
            else{
                session(['link' => $previous]);
            }
        }
        else{
             session(['link' => url()->previous()]);
        }
    
        return view('auth.login');
    }
    

    Also, Update or Overwrite protected function authenticated(Request $request, $user) in LoginController.

    protected function authenticated(Request $request, $user)
    {     
        return redirect(session('link'));      
    }
    
    0 讨论(0)
提交回复
热议问题