Laravel Passport - Automatically create token when a user is created

10,521

$tokenStr will give you the new generated token for that specific user.

$newUser = User::create();
$userObj = User::find($newUser->id);
$tokenStr = $userObj->createToken('Token Name')->accessToken;

// or try this.

$newUser = User::create();
$tokenStr = $newUser->createToken('Token Name')->accessToken;
Share:
10,521
fightstarr20
Author by

fightstarr20

Boldly going.....

Updated on June 05, 2022

Comments

  • fightstarr20
    fightstarr20 almost 2 years

    I have been looking into using Passport to allow api access to my Laravel App. I have successfully implemented it using the standard tutorial from the Laravel docs.

    I don't want an oauth management screen as described in the tutorial, I just want to be able to register a user using the App and have it automatically create the tokens it needs for that user.

    Is this possible and does anybody have an example they can point me at?