Last active
January 2, 2024 05:47
-
-
Save infostreams/1b827a688c76250e7acb7626906469a8 to your computer and use it in GitHub Desktop.
How to retrieve or authorize a User object from a Laravel Bearer API token
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use \League\OAuth2\Server\ResourceServer; | |
use \Laravel\Passport\TokenRepository; | |
use \Laravel\Passport\Guards\TokenGuard; | |
use \Laravel\Passport\ClientRepository; | |
use \Illuminate\Support\Facades\Auth; | |
use \Illuminate\Http\Request; | |
function getUser($bearerToken) { | |
$tokenguard = new TokenGuard( | |
App::make(ResourceServer::class), | |
Auth::createUserProvider('users'), | |
App::make(TokenRepository::class), | |
App::make(ClientRepository::class), | |
App::make('encrypter') | |
); | |
$request = Request::create('/'); | |
$request->headers->set('Authorization', 'Bearer ' . $bearerToken); | |
return $tokenguard->user($request); | |
} | |
function authorizeUser($bearerToken) { | |
$request = request(); | |
$request->headers->set('Authorization', 'Bearer ' . $bearerToken); | |
Auth::setRequest($request); | |
return Auth::user(); | |
} | |
$token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjI0NTU0MWVjNDE0NDk5YWRkO.......'; | |
// to get the user associated with a bearer token: | |
$user = getUser($token); | |
// to log in a user given a bearer token: | |
// (you should have an already working Laravel Passport setup with Bearer token auth for this, | |
// and please only use this in 'weird' scenarios where the default setup doesn't work and you | |
// have obtained the Bearer token through some other, external means) | |
authorizeUser($token); |
It helped me! Thanks a lot!!!
Here's another approach instead of creating a request. https://vrajroham.me/find-user-from-accesstoken/
TypeError: Argument 2 passed to Laravel\Passport\Guards\TokenGuard::__construct() must be an instance of Laravel\Passport\PassportUserProvider
TypeError: Argument 2 passed to Laravel\Passport\Guards\TokenGuard::__construct() must be an instance of Laravel\Passport\PassportUserProvider
The fix for this, at least in Laravel 7 is to change the TokenGuard parameters to this:
$tokenguard = new TokenGuard(
App::make(ResourceServer::class),
new PassportUserProvider(Auth::createUserProvider('users'), 'users'),
App::make(TokenRepository::class),
App::make(ClientRepository::class),
App::make('encrypter')
);
One thing to note is that 'users' being passed in the new PassportUserProvider line should be whatever you have set in config/auth.php as the provider in the Authentication Guards section
time saved
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's no documentation whatsoever on how to get the User object from a Bearer token, or how to authorize a user given a Bearer token. It took me half a day to put this together, so I thought I'd share the result to save someone else the trouble.