2016-11-10 45 views
0

創造AngularJS基於令牌的認證(的oauth2)和Laravel應用程序我創建它使用laravel默認註冊(AUTH)的Web應用程序,我測試過的護照的oauth2泰勒教程客戶端訪問令牌。我的web應用程序使用角js爲UI和laravel爲後端,所以我需要創建用戶,當創建用戶請求從角度發送,然後創建一個全局訪問令牌給它在我的迴應角度,然後在所有後來的請求我用它來驗證請求。如何通過護照

其實我想實現對我的web應用程序的oauth2認證,但到目前爲止,我已經搜索了很多,但我找不到教程一步它的任何有用的一步。

任何人都可以幫助我嗎?

FYI:我使用laravel 5.3護照啓用和角度JS 1.5前端。

回答

0

我已經解決了這一點。 我定製laravel權威性的登錄和註冊並創建了將請求發送到服務器上創建一個訪問令牌用於註冊用戶或登錄的方法。 我已經成立了護照並測試它作爲泰勒在做他的toturial。 然後在AuthenticatesUsers.php我已經改變了sendloginResponse方法響應,如:

protected function sendLoginResponse(Request $request) 
    { 
     isset($request->token) ? $token = $request->token : $token = null;//Check if login request contain token 

     $request->session()->regenerate(); 

     $this->clearLoginAttempts($request); 

     return $this->authenticated($request, $this->guard()->user()) 
      ? $this->StatusCode($token,$this->credentials($request),$status=false) : $this->StatusCode($token,$this->credentials($request),$status=true); 

    } 

而且我加入這個方法來請求訪問令牌並將其作爲JSON響應:

public function StatusCode($token,$user,$status){ 

     if($token != null && $token != ''){ 
      return ($status == true) ? response()->json(GetToken($user),200) : response()->json(['Message' => 'Failed to log in'],403); 

     } 

     function GetToken($userobject) 
     { 
      $http = new Client; 

      $response = $http->post('http://localhost/iranAd/public/oauth/token', [ 
       'form_params' => [ 
        'grant_type' => 'password', 
        'client_id' => '1', 
        'client_secret' => 'xKqNbzcXyjySg20nVuVLw5nk5PAMhFQOQwRTeTjd', 
        'username' => $userobject['email'], 
        'password' => $userobject['password'], 
        'scope' => '', 
       ], 
      ]); 

      return json_decode((string) $response->getBody(), true); 
     } 

     function RefreshToken($token,$userobject) 
     { 
      $http = new Client; 

      $response = $http->post('http://localhost/iranAd/public/oauth/token', [ 
       'form_params' => [ 
        'grant_type' => 'refresh_token', 
        'refresh_token' => 'refresh_token', 
        'client_id' => '1', 
        'client_secret' => 'xKqNbzcXyjySg20nVuVLw5nk5PAMhFQOQwRTeTjd', 
        'username' => $userobject['email'], 
        'password' => $userobject['password'], 
        'scope' => '', 
       ], 
      ]); 

      return json_decode((string) $response->getBody(), true); 
     } 

      return ($status == true) ? response()->json(GetToken($user),200) : response()->json(['Message' => 'Failed to log in'],403); 

    } 

爲註冊用戶相同的過程。

1

使用JWT基於令牌的認證在這裏你可以瞭解智威湯遜https://jwt.io/

+0

較短的形式找到什麼,如果我想通過自己的社會網絡在我的用戶登錄?像Facebook或谷歌加?智威湯遜是否也處理這個問題? –

0

這個職位的目的不是要回答(前面已經回答了),但給其他讀者誰最終需要對主題的更多信息更多信息。

這是非常有用的教程就在這個問題上Implementing Vue.js 2.0 and Laravel 5.3 with CORS problem solution 檢查這一個和下一個剪輯。

一些這你可以在這裏here

+0

你的答案是關於Vue.js,雖然問題是AngularJs和Laravel :)但是感謝這個信息 –