2017-12-02 191 views
0

我目前有一个身份验证功能,我在从Vue组件登录时击中。现在它会记录用户,但不会从控制器发生重定向。我不确定是否使用Vue组件导致了这一点。如果有的话,也许我可以在回复中返回预期的网址?在我login.vue组件Laravel Redirect Intended没有做任何事

public function authenticate(Request $request) 
    { 

     //Validate the login and log errors if any 
     $this->validate($request, [ 
      'email'  => 'required', 
      'password' => 'required', 
     ]); 

     //if they have stuff posted get it 
     $email  = $request->get('email'); 
     $password = $request->get('password'); 


     //See if they are actually a user 
     if (Auth::attempt(['email' => $email, 'password' => $password])) { 

      return redirect()->intended('/dashboard'); 

     } else { 
      return response()->json([ 
      'response' => 'error', 
      'error' => 'Email or Password not correct.', 
      ]); 
     } 
    } 

登录方法:

login(){ 

      this.isLoading = true; 

      this.form.post('/login') 
       .then(data => { 

        this.isLoading = false 

        if(data.response == 'success'){ 

        //Maybe get a url in the response and redirect here?? 

        } else { 
        this.serverError= data.error 
        } 

       }) 
       .catch(error => { 
        this.isLoading = false 
       }) 

      } 

使用Laravel 5.4

+1

重定向。不从laravel – C2486

+0

从Laravel重定向RESTful API没有太大意义,因为我看到它。我会返回用户实例或类似的东西,并从前端进行重定向。 – Camilo

+0

我刚刚发布了适合我的答案。我只是将预期的URL返回到我的前端,并让它处理它。谢谢@ user2486 – Packy

回答

0

,而不是使用方法的目的,为什么不使用redirect()->route()呢?

或者您正在等待URL响应。您不应该使用redirect()方法。

对于您给定的代码,您可能需要考虑这一点。

if (Auth::attempt(['email' => $email, 'password' => $password])) { 

      return response()->json([ 
       'response' => 'success', 
       'url' => Session::get('url.intended', route('route_of_your_dashboard')) 
      ]); 

     } else { 
      return response()->json([ 
      'response' => 'error', 
      'error' => 'Email or Password not correct.', 
      ]); 
     } 
2

对于任何人都希望:

在我的身份验证功能:

if (Auth::attempt(['email' => $email, 'password' => $password])) { 

      return response()->json([ 
       'response' => 'success', 
       'url' => Session::get('url.intended', url('/')) 
      ]); 

     } else { 
      return response()->json([ 
      'response' => 'error', 
      'error' => 'Email or Password not correct.', 
      ]); 
     } 

在我VUE组件登录方法从vuejs

if(data.response == 'success'){ 
        //console.log(data); 
        window.location.href = data.url 
        } else { 
        this.serverError= data.error 
        } 
+0

检查我的答案。看起来你希望得到一个URL字符串结果而不是HTML响应。因为vue是一个javascript –

+0

@KennethSunday是的,看起来像我们发布了相同类型的东西。起初我只是感到困惑,因为没有重定向发生,但意识到我需要我的前端来处理我的设置 – Packy