Skip to content

Instantly share code, notes, and snippets.

@Elshaden
Created August 4, 2022 14:45
Show Gist options
  • Save Elshaden/46e79ec1cfac7d950a0b12c9217a92f5 to your computer and use it in GitHub Desktop.
Save Elshaden/46e79ec1cfac7d950a0b12c9217a92f5 to your computer and use it in GitHub Desktop.
Custom Passport JWT Token
<?php
namespace API\Controllers;
use App\Models\User;
use DB;
use Exception;
use Laravel\Passport\Http\Controllers\AccessTokenController as ATC;
use Laravel\Passport\Http\Controllers\HandlesOAuthErrors;
use Laravel\Passport\TokenRepository;
use Lcobucci\JWT\Parser as JwtParser;
use League\OAuth2\Server\AuthorizationServer;
use Nyholm\Psr7\Response as Psr7Response;
use Psr\Http\Message\ServerRequestInterface;
use Vinkla\Hashids\Facades\Hashids;
class AccessTokenController extends ATC
{
use HandlesOAuthErrors;
/**
* The authorization server.
*
* @var \League\OAuth2\Server\AuthorizationServer
*/
protected $server;
/**
* The token repository instance.
*
* @var \Laravel\Passport\TokenRepository
*/
protected $tokens;
/**
* The JWT parser instance.
*
* @var \Lcobucci\JWT\Parser
*
* @deprecated This property will be removed in a future Passport version.
*/
protected $jwt;
/**
* Create a new controller instance.
*
* @param \League\OAuth2\Server\AuthorizationServer $server
* @param \Laravel\Passport\TokenRepository $tokens
* @param \Lcobucci\JWT\Parser $jwt
* @return void
*/
public function __construct(AuthorizationServer $server,
TokenRepository $tokens,
JwtParser $jwt)
{
$this->jwt = $jwt;
$this->server = $server;
$this->tokens = $tokens;
}
/**
* Authorize a client to access the user's account.
* Issue Toek with Custom Claim, being the user details.
*
* @param \Psr\Http\Message\ServerRequestInterface $request
* @return \Illuminate\Http\Response
*/
public function issueToken(ServerRequestInterface $request)
{
$token = $this->withErrorHandling(function () use ($request) {
return $this->convertResponse(
$this->server->respondToAccessTokenRequest($request, new Psr7Response)
);
});
$CustomToken = $this->AddUserToToken($request, $token);
$Respo = new \Illuminate\Http\Response();
return $Respo->setContent($CustomToken);
}
private function AddUserToToken($request, $token)
{
$tokens = json_decode($token->getContent(), true);
try {
$user = User::where('email', $request->getParsedBody()['username']); ;//DB::table('users')->where('email', $request->getParsedBody()['username'])->first();
$data = [
"id" => $user->id,
"name" => $user->name,
"email" => $user->email,
"confirmed" => $user->confirmed ?? Null,
"gender" => $user->gender ?? Null,
"birth" => $user->birth ?? Null,
"device" => $user->device ?? Null,
"platform" => $user->platform ?? Null,
"is_client" => $user->is_client ?? Null,
"created_at" => $user->created_at ?? Null,
"updated_at" => $user->updated_at ?? Null,
"deleted_at" => $user->deleted_at ?? Null,
"scope" => $user->scope ?? Null,
"HashedId" => Hashids::encode($user->id),
];
$tokenInfo = collect($tokens)->put('user', $data);
return $tokenInfo;
} catch (Exception) {
return $token;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment