One part of the goal here is to deepen my knowledge about the framework, so even if this isn’t “the right way”, I’d still like to try a solution to check if I should proceed with sanctum or passport.
Basic setup:
- Laravel 8.37
- Sanctum 2.14
- PHP 8
Frontend – custom login screen. Backend – authentication mechanism. (all in Laravel, but different endpoints)
Up till now, I’ve managed to:
- Build the frontend login page;
- Send the information to my API;
- My API checks user / password and creates a sanctum token for that user in
personal_access_tokens
table; - API sends information back to the frontend; Something like: loginToken: 123|xxxyyyzzz
Here’s where I’m having difficulties and not being able to access the right access route I want too.
Back in my frontend controller, I grab the token and redirect to a web “Sanctum secured” web route.
Example: AdminLoginController.php
$loginToken = $arrAuthenticationCheckJson['loginToken'];
return redirect($returnURL)
->header('Authorization', 'Bearer ' . $loginToken)
->header('Accept', 'application/json')
->with('messageSuccess', 'Login successful'));
My routes: routes\web.php
// Protected routes.
Route::group(['middleware' => 'auth:sanctum'], function () {
Route::get('/system/dashboard/',[AdminDashboardController::class, 'adminDashboard'])->name('admin.dashboard');
});
Returns me a 401 status and redirects me to the login route. I’ve watched something about XSRF-TOKEN cookie (https://youtu.be/QwjzjksfLMo), but am not sure if it interferes with anything. Also, not sure if I can set the headers this way or if I should store the returned token in a session and set the header in a middleware.
Is there any way I could make this work with my current logic?