Passport: Retrieving a personal access token after it's been created

Created on 1 Nov 2017  ·  9Comments  ·  Source: laravel/passport

My current workflow is:
1) After user registered, automatically create a Personal Access Token (PAT) to access the API
2) However once the token is created, how are we able to display the PAT again.
// Creating a token with scopes... $token = $user->createToken('Pricing Token', ['get-pricing])->accessToken;

Essentially it would be good for the user, once they have logged in to my secured site to download the Token at any time to use on their own site. Essentially I'm using the PAT to identify the user so don't need the full OAUTH workflow.

Reading through the source code, I'm able to regenerate the Access token, but how are we able to retrieve the original Token generated in Step 2.

Most helpful comment

+1

All 9 comments

+1

+1

I created this trait to grab the latest valid token for a user, with the default personal access client.

<?php

namespace App\Models\Traits;

use Laravel\Passport\ClientRepository;
use Laravel\Passport\Token;
use Laravel\Passport\TokenRepository;

trait AccessToken
{
    /**
     * Get personal access token for user.
     *
     * @return \Laravel\Passport\Token|null
     */
    public function getToken(): ?Token
    {
        return app(TokenRepository::class)->findValidToken(
            $this,
            app(ClientRepository::class)->personalAccessClient()
        );
    }
}

Why not acces it from database?

Hey there. There's a token and tokens method on the HasApiTokens trait. Is this what you need? Please note that these aren't filtered on revoked etc.

@driesvints it seems those two methods don't include personal access tokens?

Is it possible for me to retrieve the ACCESS TOKEN string from a Laravel\Passport\Token object?

Is it possible for me to retrieve the ACCESS TOKEN string from a Laravel\Passport\Token object?

I would like to know this too! In passport it is pretty easy to create tokens and clients, but not so easy (not documented well enough) to get tokens after they are created.

edit. I want to get back at my above statement. It seems that some (if not all) services (GitHub too) issue you a personal access token only once. Then you need to store it safely yourself. They usually wil let you delete previously generated tokens, but you cannot view them again. I think that this is just how it is meant to work.

Why not acces it from database?

Do you have code for this task ?

Was this page helpful?
0 / 5 - 0 ratings