Laravel get intended url

前端 未结 4 760
悲哀的现实
悲哀的现实 2020-12-31 11:08

I use a common httpRequest to login, so I could use Redirect::intended(); to lead the user to a url before them being lead to the login page. That all works we

4条回答
  •  清酒与你
    2020-12-31 11:41

    I am using the following approach with a custom login controller and middleware for Laravel 5.7, but I hope that works in any of laravel 5 versions

    • inside middleware

      if (Auth::check()){
          return $next($request);
      }
      else{
        return redirect()->guest(route('login'));
      }
      
    • if you are not passing the intented url to client side use the following inside controller login method

      if (Auth::attempt(['email' => $email, 'password' => $password])) {
      return redirect()->intended('/default');
      }
      
    • If you need to pass the intented url to client side, you can try the following

         if (Auth::attempt(['username' => $request->username, 'password' => $request->password])) {
             $intended_url= redirect()->intended('/default')->getTargetUrl();
             $response = array(
            'status' => 'success',
            'redirectUrl' => $intended_url,
            'message' => 'Login successful.you will be redirected to home..', );
            return response()->json($response);
          } else {
              $response = array(
            'status' => 'failed',
            'message' => 'username or password is incorrect', );
           return response()->json($response);
          }
      
    • Redirect to the intented url from clientside

             success: function (data) {               
                    if(data.redirectUrl){
                    window.location.href = data.redirectUrl;
                    }
             },
      

提交回复
热议问题