Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to undefined method Illuminate\Auth\TokenGuard::attempt()

Tags:

php

laravel

I'm getting this error:

Call to undefined method Illuminate\Auth\TokenGuard::attempt()

From this code:

if(Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)){
            return redirect()->intended(route('admin.dashboard'));
        }else{

I have imported Illuminate\Support\Facades\Auth as the docs suggest

My auth.php may help

<?php

return [

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
        'admin-api' => [
            'driver' => 'token',
            'provider' => 'admins',
        ],
    ],
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],
    ],
    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'admins' => [
            'provider' => 'admins',
            'table' => 'password_resets',
            'expire' => 15,
        ],
    ],
];
like image 918
Ricky Orlando Napitupulu Avatar asked Sep 01 '17 15:09

Ricky Orlando Napitupulu


2 Answers

I think you are probably trying to use a "session" guard driver instead "token". So, try to do this:

In config/auth.php configuration file:

'admin-api' => [
    'driver' => 'token',
    'provider' => 'admins',
],

You have to change it to

'admin-api' => [
    'driver' => 'session',
    'provider' => 'admins',
],

Then you should run:

 php artisan cache:clear
 php artisan config:cache

And give it a try again. Good luck!

like image 180
Felipe Barragán Avatar answered Sep 17 '22 12:09

Felipe Barragán


The token guard doesn't have an attempt method, this is a function used in session authentication. So you will need to authorize the users yourself or use the Laravel Passport authentication https://laravel.com/docs/6.x/passport

like image 20
Darkshifty Avatar answered Sep 19 '22 12:09

Darkshifty