Get users sanctum token in laravel

241 Views Asked by At

I am developing restfull API using laravel with sanctum authentication. and I also created a user dashboard to see all api consumption activities. My question is, how do I get a token from a logged in user?

enter image description here

1

There are 1 best solutions below

0
WebDesk Solution On
// Retrieve the user's Sanctum token using auth()
$user = auth()->user();
$userToken = $user->tokens->first()->token;

// Retrieve the Bearer token from the request
$bearerToken = request()->bearerToken();

// Create a new Sanctum token for the user and get the plain text token
$user = Auth::user();
$newToken = $user->createToken('MyApp')->plainTextToken;