0

So Basically we have two tables, one is the users table which is for the users of the site. The other is for people signing up for a webinar. We call this table Attendants. I want people to log into Attendant, and have Sanctum validate that, but I am running into issues. I get a 401 error.

I am using Laravel 7 Vue 2 Axios

Env

SESSION_DRIVER=database
SESSION_DOMAIN=.server.local
SANCTUM_STATEFUL_DOMAINS=server.local,localhost,127.0.0.1

Cors

    'paths' => [
        'api/*', 
        'sanctum/csrf-cookie',
        'webinars/newplatform/*'
    ],

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => true,

Guards and providers

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

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],

        'sanctum' => [
            'driver' => 'sanctum',
            'provider' => 'users',
        ],

        'sanctum_attendant' => [
            'driver' => 'sanctum',
            'provider' => 'attendants',
        ],
    ],
    
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => Modules\User\Entities\User::class,
            'table' => 'users'
        ],

        'attendants' => [
            'driver' => 'eloquent',
            'model' => Modules\Webinar\Entities\Attendant::class,
        ],
    ],

Main.js

Vue.prototype.$axios = require('axios');
Vue.prototype.$axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
Vue.prototype.$axios.defaults.baseURL = frontPath;
Vue.prototype.$axios.defaults.withCredentials = true;

Login

this.$axios.get('/sanctum/csrf-cookie').then(response => {
    this.$axios.post(`/webinar/${this.webinar}/login`, {
        email: this.email,
        password: this.password,
    }).then(({data}) => {
        //
    })
});

So, When I try to login to the attendant, I get a successful Attendant model back, but any attempts made to the sanctum routes come back as 401. Backend Login - Returns Attendant model

Auth::guard('web_attendant')->login($attendant);
return Auth::guard('web_attendant')->user();

Routes

    Route::group(['middleware' => [ 'auth:sanctum_attendant' ] ], function()
    {   
        Route::get('/webinar/{webinar}/attendant', [AuthController::class, 'attendant']);
        Route::get('/webinar/{webinar}', [PlatformController::class, 'show']);
    });

However, if i switch to login to the User Model instead of the Attendant model, the sanctum routes work

Auth::attempt(['email' => request()->email, 'password' => request()->password]);
return Auth::user();
    Route::group(['middleware' => [ 'auth:sanctum' ] ], function()
    {   
        Route::get('/webinar/{webinar}/attendant', [AuthController::class, 'attendant']);
        Route::get('/webinar/{webinar}', [PlatformController::class, 'show']);
    });

So I feel like I am missing something here. Any help would be appreciated

2 Answers2

0

I found out the Sanctum guard defaults to the web guard, which uses the Users Table. So Sanctum was trying to authenticate the Attendant with the users table.

public function __invoke(Request $request)
    {
        if ($user = $this->auth->guard(config('sanctum.guard', 'web'))->user()) {
            return $this->supportsTokens($user)
                        ? $user->withAccessToken(new TransientToken)
                        : $user;
        }

I got around this by adding

    config(['sanctum.guard' => 'web_attendant']);

To my api.php file. Since My admin panel authenticates users and webinars authenticate attendants, I can't hard code in a guard in the sanctum.php config file

0

set a different table name in config/sanctum.php

Try this solution this work for me