How to make auth:sanctum work in web.php?

Viewed 589

So my problem is that I installed Sanctum to my Laravel project and trying to use it on web routes in web.php. But when I logged in and trying to reach the location the site redirect me back to the main page and I don't know why this happens. The site should redirect me back when I'm not logged in.

Route::get('/support', function () {
    return view('support');
})->middleware("auth:sanctum");

This is the code which I'm using in the web.php file. I think that the middleware somehow doesn't get the user loginToken which is created after the login, but I don't know where to fix it.

2 Answers

Have you added gurds for web,

config/Sanctum.php

/*
    |--------------------------------------------------------------------------
    | Sanctum Guards
    |--------------------------------------------------------------------------
 
    */

    'guard' => ['web','api'],

In auth.php file change guard from web to sanctum like this:

'defaults' => [
    //'guard' => 'web',
    'guard' => 'sanctum',
    'passwords' => 'users',
],

Then you can apply ->middleware("auth:sanctum") on all routes.

Related