When you made configuration changes, did you restart the server?
Laravel Sanctum auth:sanctum error Allowed memory size of 536870912 bytes exhausted
So i am using Laravel Sanctum on this version
- Laravel 11.28
- Sanctum 4.0.3
- PHP 8.3.9
I created an api routes of login just to create a token using createToken() and the process is working just fine with the access token is successfully stored in the personal_access_token in my database. I have configured the Sanctum config just like as explained in the Laravel Sanctum Documentation where:
config/sanctum.php
'guard' => ['api'],
config/auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'sanctum',
'provider' => 'users',
],
],
routes/api.php
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
Route::get('/user', function (Request $request) {
return $request->user();
})->middleware('auth:sanctum');
Route::post('/login', function (Request $request) {
$credentials = $request->only('email', 'password');
if (! Auth::attempt($credentials)) {
return response()->json(['message' => 'Invalid credentials'], 401);
}
$user = Auth::user();
$token = $user->createToken('login-token')->plainTextToken;
return response()->json(['token' => $token]);
});
and when i hit the /api/user with any request (even empty request) it shows a HTTP 500 laravel error of Allowed memory size of 536870912 bytes exhausted (tried to allocate 60821504 bytes). I have tried adjusting the memory limit to use more memory, but it keeps getting the same error so I wondering is it something like infinite loops somewhere. I have tried to remove the middleware auth:santum and it is working just fine.
I tried to revert the guard in config/sanctum.php to web, and it is working when I passed the created token in the Bearer (Positive Case Testing) but when the auth is incorrect I am getting the page redirected to login where I dont have the routes and it shows laravel error of Route [login] not defined.
I do want to use the api guard in the config/sanctum.php but the error seems so random to me and i can't find what is wrong even with a simple code. And the web guard seems to be not suitable for my case where I need api response just to be responding to something like Unauthorized message and not redirecting to a login page just like what web routes would do.
Have any suggestion on how my code is somewhere incorrect or just ask if you want me to provide something more in code.
Please or to participate in this conversation.