Laravel Passport Print Personal Access Token

17,163

Solution 1

I think you should just generate the token before or at the same time as you're creating a user and store it in the database:

Add the column:

$table->string('token', 60)->unique();

Save the token:

$token = $user->createToken('android')->accessToken;

$user = User::create([
    'name' => $data['name'],
    'email' => $data['email'],
    'password' => bcrypt($data['password']),
    'token' => $token,
]);

Then it will be available as:

$user->token;

Solution 2

i have face similar problem with laravel & vue js i i updated my middleware handler allow to access Authorization token. it working find for me. some time it will be help Laravel Passport 401 Unauthorized Error using Apache and Vue

Solution 3

You can try the following code:

    $user->tokens->load('client')->filter(function ($token) {
        return ! $token->client->firstParty() && ! $token->revoked;
    })->values();

Solution 4

You can't retrieve access_token directly. Because, when you create access token $token = $user->createToken('android')->accessToken;, Laravel Passport will create access_token and it is not directly store into database. Instead, it is store that access_token's id(called Json Token Id) into oauth_access_tokens table.

So, when you search access_token directly in oauth_access_tokens table, you will never find that access_token, only you can see its Json Token Id.

Eg: access_token: eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiNTMxYTAyYTBkMjA4N2MyZWI3NDU1MWM0NzkzZGM3OTJkYWM3ZmRmYTcyOTVjZGI1NWNjMDg4NDFiMDUyZTc5M2ViMTE5NDg3ZmNhYWUxODciLCJpYXQiOjE2MjMxMjU3MjYuNjgyMzE2LCJuYmYiOjE2MjMxMjU3MjYuNjgyMzIsImV4cCI6MTY1NDY2MTcyNi42NzQwMzYsInN1YiI6IjQiLCJzY29wZXMiOltdfQ.hCfn7mPgrNoD-kHXjgXawoil50jBzh78s_7MBq-GRUOD7fNKzEn-24mHT_qppu5v-TAlJSww0iR-zPCe82_FB4JlmR0D1ERwVdX_DD1wKcn2DR9mhCKdB8XNLsu4MZMqhPahO7ft2mC8Hu7lM6zhfgKiAkiNsR68zUbCLYB7_h82T-ef5Xp6Lp61wfSq_KATVpEkv59jp62PSdNyF3SLPT5bqfOOwziJxv2lMW1Y61M4WY_3f4prwVfA81qo_XkczBC4b9-j36ly7YluzbVHRQnNGzTRkekv8fuv4Q3USRdRkWDFR2lTJ9zz31LEN8jbnY9hoAkvE57KyEyJ3qfUzkoYFXaAIF9VQe6j8TtNGOehiAf9uQm049m0zopL5w-g4u5qulJeYp0OEgfq6nK8DtuAERSgEAeY2kINqbLenhywwJmX70mrF_BqfxaZS7MIomyybOWi6FVHj4WXA8OIPgrUUu2BAtgwdCtt-ECN6svCvBLV15nBXi6OPpbSFbbV2Ve4fy2kGH5dWUfKZe0W6Cai0Uux_lUVDSx6q6bo4bf5_67Twg2E7EI4CpzyCk7g_ZG3Ff3vdTTs8P5f0LIHihCjCp6c_cuLnws8laD1L1-rqBmCQSZ7ZGeI-LYouWEbtXnf6M4xlfJoubXZGiTi8Zun9X2EhgRR8XjviOWM8AU

Its Json Token Id: 87dcc72560f41406d6cec1f35a66c24ac65953f2e8b28401e06e282c340eba7ef2c2fee65a0a0519

Share:
17,163
Minion
Author by

Minion

Updated on June 05, 2022

Comments

  • Minion
    Minion almost 2 years

    I am using Laravel's passport package to provide token based authentication to my rest api. Right now, I am using personal access token concept to generate the access token.

    To generate an access token for a single user, I am using below code to generate a token with name 'android'.

        $user = User::create([
                    'name' => $data['name'],
                    'email' => $data['email'],
                    'password' => bcrypt($data['password']),
                ]);
    
        // Here the access token will be stored in $token variable.
        $token = $user->createToken('android')->accessToken;
    
        // Now the $token value would be something like
       //eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImMyNjI3YzU0YjFhNWIxZTFlMTdkODhmZTk1NzhjNzAzY2QyMTU0MzhlOD...
    

    Later on I want to display the personal access token on my admin dashboard which I am facing difficulty in getting the generated token again. Tried below code, but couldn't able to get the access token.

    $user = User::find(1)
    dd($user->tokens())
    

    I also tried using passport vue elements, but it is displaying just the access token name, not the actual token.

    <passport-personal-access-tokens></passport-personal-access-tokens>
    

    Please help me getting this solved.

    Thank you

  • channasmcs
    channasmcs over 7 years
    some time this will be right missing 'token' => $token, will be reason.
  • Minion
    Minion over 7 years
    Printing empty array.
  • Minion
    Minion over 7 years
    I have done this already, but i don't want to duplicate this token again as it is already stored somewhere in passport tables.
  • Denis Kovacevic
    Denis Kovacevic over 7 years
    @Minion try this: $user->tokens->load('client')->filter(function ($token) { return $token->client->personal_access_client && ! $token->revoked; })->values();