2017-01-12 33 views
6

我已經安裝laravel 5.3護照包裹。 I,接着步驟 的documentaition步驟I可以使用下面的路線POST /oauth/token使用以下參數使用laravel護照從api路線註冊新用戶

  1. 用戶名
  2. 密碼
  3. client_secret
  4. grant_type
  5. CLIENT_ID

和我收到以下回復

{ 
    "token_type": "Bearer", 
    "expires_in": 31536000, 
    "access_token": "access token here", 
    "refresh_token": "refresh token here" 
} 

然後我請求GET /api/user

與下面的頭

  1. 授權=
  2. 「承載令牌這裏訪問」 接受=應用/ JSON(可選)

這工作很好,所以所有的apis。

我的問題是誰我authinticated,並在第一個請求進入了他的用戶名和密碼,並返回我回訪問令牌的用戶是我從laravel網絡視圖中創建一個用戶/註冊

我怎麼能創建新的用戶或從API路由文件

註冊新用戶喜歡POST /api/register

在用戶第一次需要註冊後要authinticated。

我是否應該創建這條路線沒有oauth註冊然後如果成功註冊他請求POST /oauth/token被認證或什麼? 我錯過了什麼?

更新clent_secret是它的權利是不斷在所有用戶請求或每個用戶應該有不同勢clent_secret,如果是如何創建aclent祕密,如果它neaded到authinticate用戶?

回答

0

要做到這一點,最快的方式是增加一個例外,你verifyCsrfToken.php類

protected $except = [ 
     'register' 
    ]; 

然後你可以張貼到您的註冊模式,並隨後訪問該帳戶的OAuth /令牌。

0

如果我正確理解您的問題,您需要註冊新用戶並從/oauth/token註冊後獲得令牌。爲此,您可以使用代理。我使用了類似的東西,我遵循以下步驟。這既適用於登錄和註冊

  1. 註冊用戶,並從您的註冊方法來發送一個HTTP請求到/oauth/token端點

    public function register(RegisterRequest $request) 
    { 
        $user = User::create($request->all()); 
    
        $response = $this->authenticationProxy->attemptLogin($request->email, $request->password); 
        return response()->json(compact('response', 'user'), 201); 
    } 
    
  2. 然後在您的proxy類,調用/oauth/token

    public function attemptLogin($email, $password) 
    { 
        $http = new GuzzleHttp\Client; 
    
        $response = $http->post('http://your-app.com/oauth/token', [ 
         'form_params' => [ 
          'grant_type' => 'password', 
          'client_id' => env('CLIENT_ID'), 
          'client_secret' => env('CLIENT_SECRET'), 
          'username' => $email, 
          'password' => $password, 
          'scope' => '', 
         ], 
        ]); 
    
        return json_decode((string) $response->getBody(), true); 
    } 
    

希望這會他LPS。您也可以使用類似的方法進行登錄。