[英]Custom Token Response in Laravel Passport
The default response of Laravel Passport comes like this: Laravel Passport 的默认响应是这样的:
{
"token_type": "Bearer",
"expires_in": 31536000,
"access_token": djabjkdakadbakdkakjdbjkba
}
I need to add some custom response like:我需要添加一些自定义响应,例如:
{
"success":"true",
"message":"Login Successful",
"status":"200",
"data":{
"token_type": "Bearer",
"expires_in": 31536000,
"access_token":"djabjkdakadbakdkakjdbjkba"
}
}
Can somebody help me about this idea?有人可以帮我解决这个想法吗?
I have been banging my head for hours until I found the solution. 我一直在敲我几个小时直到找到解决方案。 This currently works for my version of Laravel 5.5.33:
这适用于我的Laravel 5.5.33版本:
Add a new route in your api.php file. 在api.php文件中添加新路由。
Route::post('oauth/token', 'AccessTokenController@issueToken');
Create new file in YourProject/app/Http/Controllers/AccessTokenController.php 在YourProject / app / Http / Controllers / AccessTokenController.php中创建新文件
<?php namespace App\\Http\\Controllers; use App\\User; use Exception; use Illuminate\\Database\\Eloquent\\ModelNotFoundException; use League\\OAuth2\\Server\\Exception\\OAuthServerException; use Psr\\Http\\Message\\ServerRequestInterface; use Response; use \\Laravel\\Passport\\Http\\Controllers\\AccessTokenController as ATC; class AccessTokenController extends ATC { public function issueToken(ServerRequestInterface $request) { try { //get username (default is :email) $username = $request->getParsedBody()['username']; //get user //change to 'email' if you want $user = User::where('username', '=', $username)->first(); //generate token $tokenResponse = parent::issueToken($request); //convert response to json string $content = $tokenResponse->getContent(); //convert json to array $data = json_decode($content, true); if(isset($data["error"])) throw new OAuthServerException('The user credentials were incorrect.', 6, 'invalid_credentials', 401); //add access token to user $user = collect($user); $user->put('access_token', $data['access_token']); //if you need to send out token_type, expires_in and refresh_token in the response body uncomment following lines // $user->put('token_type', $data['token_type']); // $user->put('expires_in', $data['expires_in']); // $user->put('refresh_token', $data['refresh_token']); return Response::json(array($user)); } catch (ModelNotFoundException $e) { // email not found //return error message return response(["message" => "User not found"], 500); } catch (OAuthServerException $e) { //password not correct..token not granted //return error message return response(["message" => "The user credentials were incorrect.', 6, 'invalid_credentials"], 500); } catch (Exception $e) { ////return error message return response(["message" => "Internal server error"], 500); } } }
Done! 完成! you can customize whatever you want over here, with whatever conditions you have.
您可以根据自己的条件自定义您想要的任何内容。 This was a lazy approach to not make your own PassportServiceProvider.
这是一种不做自己的PassportServiceProvider的懒惰方法。 Credits go to: nauvalazhar https://gist.github.com/messi89/489473c053e3ea8d9e034b0032effb1d
积分转至: nauvalazhar https://gist.github.com/messi89/489473c053e3ea8d9e034b0032effb1d
There is a method called getExtraParams
in BearerTokenResponse
class. BearerTokenResponse
类中有一个名为getExtraParams
的方法。
/**
* Add custom fields to your Bearer Token response here, then override
* AuthorizationServer::getResponseType() to pull in your version of
* this class rather than the default.
*
* @param AccessTokenEntityInterface $accessToken
*
* @return array
*/
protected function getExtraParams(AccessTokenEntityInterface $accessToken)
{
return [];
}
By overriding it your problem will be solved.通过覆盖它,您的问题将得到解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.