5

Currently I am building rest API using Laravel. For authentication, I am using the Sanctum package. Every time a user is logged in it generate a token that looks like this:

"token": "98|b45h97e17VVpugjO71wwURoicIqDQP2ejTkCWwoD" 

But why Sanctum includes the database id with the token?

enter image description here

How to remove the database id from the token?

Biswajit Biswas
  • 859
  • 9
  • 20
  • Any help will be appreciated! – Biswajit Biswas Sep 07 '21 at 08:54
  • I would also like to learn how to do this. It doesn't serve my purposes to have the ID prepended to the token, but I don't want to mess up Sanctum's token verification by manually editing it after creation. – Drowsy Mar 25 '22 at 13:23

2 Answers2

2

I just looked through the source history and found that the ID was introduced in a well-named commit called more performant tokens lookup, so that is the reason the ID is part of the token.

But if you look at the code loading/verifying the token there is a fallback at the beginning in case there is no ID. So you can simply remove it from the token, for example by overriding the findToken method.

Thomas
  • 8,426
  • 1
  • 25
  • 49
1

try this,

Option 1:-

In Controller:-

$token = $user->createToken(''project_name')->plainTextToken;
$auth_token = explode('|', $token)[1];

Option 2:-

In postman Refer image:-

enter image description here

Yudiz Solutions
  • 4,216
  • 2
  • 7
  • 21