Skip to content

Instantly share code, notes, and snippets.

@infostreams
Last active January 2, 2024 05:47
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save infostreams/1b827a688c76250e7acb7626906469a8 to your computer and use it in GitHub Desktop.
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
<?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);
@infostreams
Copy link
Author

infostreams commented Oct 30, 2019

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.

@NayanJD
Copy link

NayanJD commented May 2, 2020

It helped me! Thanks a lot!!!

@vrajroham
Copy link

Here's another approach instead of creating a request. https://vrajroham.me/find-user-from-accesstoken/

@zhaochong
Copy link

TypeError: Argument 2 passed to Laravel\Passport\Guards\TokenGuard::__construct() must be an instance of Laravel\Passport\PassportUserProvider

@mwaboff
Copy link

mwaboff commented Nov 17, 2020

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

@k1-end
Copy link

k1-end commented Jan 2, 2024

time saved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment