Eahiya
Eahiya

Reputation: 1321

Laravel Passport createToken method not giving me access_token. How can I solve this problem?

All of configuration is right, but does not work.

My controller code is:

 $appUser = User::create([
                'name' => $user->name,
                'email' => $user->email,
                'password' => Hash::make(Str::random(8)),
            ]);

return $passportToken = $appUser->createToken('Token Name')->accessToken;

and response code from this URL (http://localhost:8000/api/authorize/github/callback?code=6ba82d59f00fdb1ad5c9) is:

{
  "name": "Token Name",
  "abilities": [
    "*"
  ],
  "tokenable_id": 1,
  "tokenable_type": "App\\Models\\User",
  "updated_at": "2021-09-19T05:10:00.000000Z",
  "created_at": "2021-09-19T05:10:00.000000Z",
  "id": 3
}

Upvotes: 1

Views: 1489

Answers (2)

Aleksandr Taran
Aleksandr Taran

Reputation: 63

By Default User Model use HasApiToken provided by Laravel\Sanctum\HasApiTokens trait, please check User Model and if it point to Sanctum update it to use Laravel\Passport\HasApiTokens;

Upvotes: 2

Laravel Passport uses its own database migration directory, so you should migrate your database after installing the Laravel Passport package. The Passport migrations will create the tables your application needs to store clients and access tokens:

php artisan migrate

If you have any problems, tell me.

Upvotes: 0

Related Questions