The Laravel framework is an open-sourced PHP web framework that allows developers to create dynamic and scalable web applications. The source code of Laravel is hosted on GitHub and released under the MIT license.
Questions tagged [laravel-sanctum]
585 questions
5
votes
2 answers
Laravel - Sanctum Remove Database Id From Generated Token
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…

Biswajit Biswas
- 859
- 9
- 20
5
votes
2 answers
Customize laravel sanctum unauthorize response
I am using laravel sanctum in my project, but I am facing a problem. I want to customize the 401 response code (unauthorized) to return a JSON when a token is invalid, something like this:
{
"data": {
"code": 401,
"book":…

Denny Kurniawan
- 1,581
- 2
- 15
- 28
5
votes
1 answer
How can i handle both SPA and token based authentication with Laravel Sanctum
It's been several days now that I spend on Laravel Sanctum trying to figure out how I can set up my authentication system.
Context
I'm building a tickets web app for my company and for this i decided to build an API and a React frontend app that…

adrien
- 115
- 1
- 6
5
votes
1 answer
Laravel Sanctum Tokens - Two Databases
I am having some problems getting Laravel Sanctum authorising two tables in two separate databases.
I am using Laravel Sanctum tokens for authorisation. I have two tables to authorise users (users & contacts) I have setup two separate guards and can…

markwilliamsweb
- 470
- 6
- 15
5
votes
1 answer
Sanctum form request validation on fail redirect to home
I am creating API with sanctum, and works fine I can list users, update, store, etc.
I added middleware auth:sanctum and passing token, and I add middleware EnsureFrontendRequestsAreStateful like explained in docs.
With invalid token doesn't work…

Zoroaster
- 63
- 1
- 7
5
votes
4 answers
Retrieve user by Sanctum plainTextToken
How to retrieve the 'logged in' user from a Sanctum token.
For logging in I have the following method
public function login(Request $request)
{
if (Auth::attempt($request->toArray())) {
/* @var User $user */
$user =…

Ezrab_
- 825
- 5
- 19
- 44
5
votes
2 answers
Testing Laravel Sanctum acting as results in bad method call
I'm trying to test an authenticated API route which only an authenticated user can post to a specific route.
Looking at the Laravel Sanctum docs, I can use the code below to create and authenticate a user:
Sanctum::actingAs(
…

basic
- 219
- 6
- 12
4
votes
1 answer
Use laravel sanctum session-bases with next.js in sever side (like getServerSideProps metod)
I have laravel as backend and next.js as frontend of my website.
I use laravel sanctum for authentication.
My laravel app and next.js app are in the same host, then I can use session-base sanctum for authentication without use token.
After login…

Esmaeil Soomari
- 130
- 2
- 9
4
votes
1 answer
Laravel Sanctum token expiration based on last usage
I have set the sanctum token expiration in the config file, for let's say 24 hours:
/*
|--------------------------------------------------------------------------
| Expiration…

Dusan
- 3,284
- 6
- 25
- 46
4
votes
0 answers
Axios and NextJS + Sanctum cookie: it's discarding baseURL and call wrong domain route
I have an API built with Laravel and Sanctum.
Front-end is built in NextJS. CORS and communication is doing fine - apparently, I can't see errors even with POST requests.
Login goes fine, Laravel sets a cookie for .mydomain.com, path "/", and it's…

ppalmeida
- 2,924
- 5
- 20
- 25
4
votes
2 answers
Error 502 trying to use Laravel Sanctum with custom model
I'm using Laravel sanctum to authenticate my API, but i'm not using the default laravel User model, I've built a model to separate my App customers in another table called Customer. To this to work I did the alterations bellow
Custom model…

Cesar Henrique Damascena
- 231
- 5
- 17
4
votes
3 answers
Why am I getting a CSRF token mismatch with Laravel and Sanctum?
I have a Laravel backend and react frontend where I use sanctum for SPA authentication and Axios to make my requests. I am using localhost with my backend on http://localhost:8000 and frontend on http://localhost:3000.
Here is my frontend…

Patrick Obafemi
- 908
- 2
- 20
- 42
4
votes
2 answers
How to optionally authenticate with Laravel Sanctum?
I'm using Laravel Sanctum to authenticate users. I'd like to have a route that can be accessed by guests and logged in users. Logged in users send an API Token in the Authorization header.
I've tried making a route without authentication, but that…

Gergő Gutyina
- 129
- 1
- 12
4
votes
3 answers
laravel sanctum Route [login] not defined
I am trying to get the current user in my laravel vue application. And whenever I trie to get it i am getting this error:
Symfony\Component\Routing\Exception\RouteNotFoundException: Route [login] not defined. in file…

Moemen Hussein
- 394
- 5
- 16
4
votes
4 answers
Get 401(Unauthenticated) error after login on laravel sanctum with nuxtjs
I have installed:
laravel v 7.30.4
nuxtjs v 2.15.7
After i login in my laravel sanctum with nuxtjs auth module, when nuxt try to get user, laravel response 401 error(Unauthenticated message).
My network status:
My cookies status:
api.php:
…

Omid Reza Heidari
- 658
- 12
- 27