Can you show the api.php file?
Laravel API with session driver and Sanctum
Hi guys. I'm trying to implement Sanctum with no luck, I'm getting an Unauthorized message doesn't matter what I do. I'm after reading every single article and watching streams, but still no luck.
The official documentation is very unclear as well.
CORS works well, I'm setting and getting the XSRF cookies well, those are present both in response and request.
The docs say, for a SPA (I use Angular) I should use the SPA flow and not the API token flow.
On the other hand, the documentation requires sending the login request to the web middleware group (/login) instead of the API (/api/login) which I don't understand why.
Why I would call the web middleware when I need to call an API endpoint from my frontend application? This feels wrong.
Also, the docs below say, for mobile applications, I should use the token flow, but different than the API token flow. So strange this too. No further explanation.
I've read a lot about security issues with storing the API token in localstorage, it's not recommended, that's why I would like to go with the session flow using cookies and not locally stored tokens with Auth header
My question is:
How to use /api/* endpoints with using the session driver and NOT the token flow (which is not recommended anymore)?
SANCTUM_STATEFUL_DOMAINS=192.168.88.10:4200
SESSION_DOMAIN=192.168.88.10
SESSION_DRIVER=cookie
SESSION_LIFETIME=120
## TODO: On production (https) set this true
SESSION_SECURE_COOKIE=false
Route::group(['prefix'=>'v1','as'=>'v1.'], function () {
Route::get('/login', [UserController::class, 'login']);
Route::group(['middleware' => ['auth:sanctum', 'throttle:500|2200,1']], function () {
Route::get('/status', [AppController::class, 'getStatus']);
Route::resource('users', UserController::class)->middleware('web');
Route::get('/logout', [UserController::class, 'logout']);
});
});
public function login(Request $request): Response
{
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
$user = Auth::user();
return Response::data($user)
->message('Login was successful!');
}else{
return Response::message('Wrong username or password!','error',401);
}
}
Before I used JWT but it seems it's not secure anymore since it's about storing the token in the local storage.
After hours of trying together with the guys on the Discord channel, they made me drop Sanctum entirely, Sanctum package is a mess, and my feeling was right with this web/api routing mess up... I felt it wrong because the whole idea is wrong in this way.
Finally (thx to the suggestion on the channel), I ended up using Laravel session authentication without any 3rd party package and works like a charm:
I've added a session middleware group in Kernel.php
'session' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
],
changed the middleware in RouteServiceProvider
$this->routes(function () {
Route::prefix('api')
->middleware('session')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
created routing group with auth middleware
Route::group(['middleware' => ['auth', 'throttle:500|2200,1']], function () {
.
.
.
)}
This will do it. No JWT, no Sanctum, token in a httpOnly cookie (most secure way compared to other flows like storing token in the localstorage), it's perfect.
Please or to participate in this conversation.