Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

codeXX's avatar
Level 1

Issue with Token Authentication using Sanctum and MongoDB

I was playing around with Laravel, Nuxt 3 and Sancutm, Basically following this link. The part where a custom middleware is used instead of auth:sanctum to ensure the user is logged in using valid token surprises me because with MySQL backend auth:sanctum middleware works just fine no need for a custom middleware to ensure the user is logged in for each request. Is there another model that needs modification in order to work properly?

1 like
4 replies
vincent15000's avatar

with MySQL backend auth:sanctum middleware works just fine no need for a custom middleware to ensure the user is logged in for each request

Hmmm I'm not sure that you're right.

With auth:sanctum the application checks if the user is logged in for each request, it's only done implicitely.

With Sanctum, you don't need to use a custom middleware, just protect the route with auth:sanctum.

codeXX's avatar
Level 1

@vincent15000 Try it, if the database is MySQL it will implicitly log in the user, with MongoDB it doesn't.

1 like
codeXX's avatar
Level 1

@vincent15000 by adding the lines below to PersonalAccessToken custom class it worked fine.

	protected $table = 'personal_access_tokens';
    protected $keyType = 'string';
1 like

Please or to participate in this conversation.