Friday, April 14, 2017

return access token and user when authenticate passport laravel

I want ask something what make me confused. I was create API with Laravel 5.4 and passport. This API authorization with password grant type.

Sample request like this.

$http = new GuzzleHttp\Client;
$response = $http->post('http://your-app.com/oauth/token', [
  'form_params' => [
     'grant_type' => 'password',
     'client_id' => 'client-id',
     'client_secret' => 'client-secret',
     'username' => 'taylor@laravel.com',
     'password' => 'my-password',
     'scope' => '',
   ],
]);

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

This request send post to /oauth/token

And i will get response like this.

{
  "token_type": "Bearer",
  "expires_in": 3155673600,
  "access_token": "eyJ0eXAiOiJK...",
  "refresh_token": "LbxXGlD2s..."
}

But i want directly get response with authenticated user like this, so i will not twice request when login to get user detail info. I hope response like this.

[
  data:{
     name: Jhon,
     email: jhon@example.com,
     address: ASDF Street no.23
  },
  token{
     "token_type": "Bearer",
     "expires_in": 3155673600,
     "access_token": "eyJ0eXAiOiJK...",
     "refresh_token": "LbxXGlD2s..."
  }
]

I hope you can help me, and tell me how to do this. thanks all.



via Egi Nugraha

Advertisement