2017-12-02 189 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