D
D
dmalapsh2021-09-15 22:39:54
Laravel
dmalapsh, 2021-09-15 22:39:54

How to check if a user is logged in when using laravel sanctum?

The problem is that when using the standard Auth::check() construct, false is always returned. As I understand it, this is due to the fact that sanctum reads the token only when middleware is hung on the route. But I need the user to be not authorized, depending on this, I perform different actions

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dmalapsh, 2021-09-16
@dmalapsh

As it turned out, I just poorly configured the application. The fact is that in config/auth.php it is necessary to set the default authorization method to api, and the verification driver to sanctum

'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
    ],

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'sanctum',
            'provider' => 'users',
            'hash' => true,
        ],
    ],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question