-
-
Save messi89/489473c053e3ea8d9e034b0032effb1d to your computer and use it in GitHub Desktop.
| <?php | |
| /** | |
| * Laravel Passport - Customize Token response. | |
| * | |
| * @author Messi89 OVERGEN <@messi89minou> | |
| * @link https://github.com/messi89 | |
| */ | |
| namespace App\Http\Controllers\Api; | |
| use App\Models\User; | |
| use Exception; | |
| use Illuminate\Database\Eloquent\ModelNotFoundException; | |
| use League\OAuth2\Server\Exception\OAuthServerException; | |
| use Psr\Http\Message\ServerRequestInterface; | |
| use Response; | |
| class AccessTokenController extends \Laravel\Passport\Http\Controllers\AccessTokenController | |
| { | |
| public function issueToken(ServerRequestInterface $request) | |
| { | |
| try { | |
| //get username (default is :email) | |
| $username = $request->getParsedBody()['username']; | |
| //get user | |
| $user = User::where('email', '=', $username)->firstOrFail(); | |
| //issuetoken | |
| $tokenResponse = parent::issueToken($request); | |
| //convert response to json string | |
| $content = $tokenResponse->getBody()->__toString(); | |
| //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']); | |
| return Response::json(array($user)); | |
| } | |
| catch (ModelNotFoundException $e) { // email notfound | |
| //return error message | |
| } | |
| catch (OAuthServerException $e) { //password not correct..token not granted | |
| //return error message | |
| } | |
| catch (Exception $e) { | |
| ////return error message | |
| } | |
| } | |
| } |
is there a way to override the original AccessTokenController so that i do not have to create a new api/oauth/token route?
i just want to override the issueToken method in AccessTokenController so that i still hit the oauth/token endpoint. I have already created a new class AccessTokenController that extends from \Laravel\Passport\Http\Controllers\AccessTokenController but that does not override the method.. am i missing something? @messi89
The best way I have found to do this is by extending the
BearerTokenResponseclass - see this answer on stack overflow
I would say, this is a clean solution. I tested in laravel 5.8 and it is working.
Hello everybody.
Please give an answer you are 100% sure is a real, well tested and accepted solution. Otherwise you make a mess and confusion among people who wants to learn implementing OAuth in a good and secure way. Some writings make a lot of confusion and misunderstanding. All things related to implementation and customizing OAuth token are already present in laravel/passports package. You need nothing more than that. Just exam the package and documentation on Laravel official site.
Hi Messi, it worked for me. Thanks for the help.
Hi there ๐, Laravel 8.54 with Passport 10.1, nice job Messi!!!
<?php
namespace App\Http\Controllers\Auth;
use Exception;
use App\Models\User;
use Psr\Http\Message\ServerRequestInterface;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Laravel\Passport\Exceptions\OAuthServerException;
use Laravel\Passport\Http\Controllers\AccessTokenController as AuthController;
class AccessTokenController extends AuthController
{
public function issueToken(ServerRequestInterface $request)
{
try {
$data = json_decode(parent::issueToken($request)->content(), true);
$user = User::select(["name", "email"])
->where('email', '=', $request->getParsedBody()['username'])
->firstOrFail()
->toArray();
return response()->json(array_merge(["user" => $user], $data));
} catch (ModelNotFoundException $e) {
return response()->json(array(
'error' => array(
'msg' => $e->getMessage(),
'code' => $e->getCode(),
),
), 401);
} catch (OAuthServerException $e) {
return response()->json(array(
'error' => array(
'msg' => $e->getMessage(),
'code' => $e->getCode(),
),
), 401);
} catch (Exception $e) {
return response()->json(array(
'error' => array(
'msg' => $e->getMessage(),
'code' => $e->getCode(),
),
), 500);
}
}
}
@bakiro is that running on Laravel 8.83.27? the route on /oauth/token always retrun null
Thanks for the solution!
@leandroruel
You can find the new route at /api/oauth/token