A
A
Alexander Koregin2020-06-10 21:49:33
Laravel
Alexander Koregin, 2020-06-10 21:49:33

How to make two different authorizations in Laravel?

I am trying to make two different authorizations. One for users, the other for administrators.

I did it with Guard, but it gives me an error, how can I fix it

auth.php

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

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

        'customers' => [
            'driver' => 'token',
            'provider' => 'customers',
            'hash' => false,
        ],
    ],

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],

        'customers' => [
            'driver' => 'eloquent',
            'model' => App\Customer::class,
        ],
    ],

And the Controller itself
if (Auth::guard('customers')->attempt($data)) {
            $user = Customer::find(Auth::user()->id);

            $user->api_token = Str::random(32);
            $user->save();

            if ($user->api_token == "") return response()->json([], 200);

            return response()->json(['api_token' => $user->api_token], 200);
        } else {
            return response()->json(['login' => 'Неправильно указан логин или пароль'], 404);
        }

And the error that arrives
Error: Call to undefined method Illuminate\Auth\TokenGuard::attempt() in file /home/a0419894/domains/rostovrepair161.ru/public_html/api/app/Http/Controllers/Auth/CustomerController.php on line 63

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
Xcho Margaryan, 2020-06-17
@dragonika8

in guards[] of customer, change the driver to session.
and add api for it
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token ',
'provider' => 'users',
'hash' => false,
],
'customers' => [
'driver' => 'session',
'provider' => 'customers',
'hash' => false ,
],
'
'hash' => false,
],
],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question