MrFiliper's avatar

Method attempt does not exist or Auth guard driver [api] is not defined.

I use Lumen 5, and I'm having trouble with using authentication.

I have uncommented

$app->register(App\Providers\AuthServiceProvider::class);
$app->withFacades();
$app->withEloquent();

in app.php but authentication not work /

    public function postLogin(Request $request)
    {
        $this->validate($request, [
            'email'    => 'required|email',
            'password' => 'required',
        ]);

        $credentials = $request->only('email', 'password');

        if (Auth::attempt($credentials, $request->has('remember'))) {
            return ['result' => 'ok'];
        }

        return ['result' => 'not ok'];
    }

If I create a request to /login with params need to succesful request, display error:

Method attempt does not exist.

Ok, next I create new auth.php config

<?php

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

    'guards' => [
        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
    ],

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

and display next error: Auth guard driver [api] is not defined.

Why? The api guard is defined in the config?!

Thanks for replay!

0 likes
8 replies
mils's avatar

bump, same issue here on a brand new project (Laravel 5.5, passport 4.0)

cent040's avatar

use check() instead of attempt. It's work for me

if (Auth::check(['email' => $email, 'password' => $password,false, false])) {
        $user = Auth::user();
        $data = array(
        "is_login" => true,
        "name" => $user->name,
        "api_token" => $user->api_token
        );
        return Response::json(
            array(
            'status' => true,
            'data' => $data,
            'msg' => "Login Successfully"
            ), 200
        );
    }
3 likes
lucastoneatto's avatar

Hi, same issue here on a brand new project (Laravel 5.5, passport 4.0). I used Check, but always return false. I think that hash password not same at generated in register. what could be the error?

WisdomEye's avatar

After add:

use Laravel\Passport\HasApiTokens;

To my Login Controller, the issue was gone.

The basic LoginController:

<?php

namespace App\Http\Controllers\API\Auth;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Laravel\Passport\HasApiTokens;
use Auth;
use Validator;
use App\User;


class LoginController extends Controller
{
    public $successStatus = 200;

    /*
    * API login user and create token
    *
    * @param  [string] email
    * @param  [string] password
    * @return [string] access_token
    */

    public function userLogin(Request $request)
    {
        $validator = Validator::make($request->all('email', 'password'), [
            'email' => 'required|string|email|max:255',
            'password' => 'required|string|min:6'
        ]);

        if ($validator->fails()) {
            return response()->json(['error'=>$validator->errors()], 401);
        };

        if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){
            $user = Auth::user();
            $success['token'] = $user->createToken('MyApp')-> accessToken;
            return response()->json(['success' => $success], 200);
        }
        else{
            return response()->json(['error'=>'Can not log in with the data provide.'], 401);
        }
    }
}

Ignore this reply, worked for an unknow wierd reason, and after clear cache, does not work anymore...

WisdomEye's avatar

I don't know why, got back working again.

The thing I have noticed is the default on config/auth.php setting for:

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

Got it working once more time.

For make sure, have deleted everything on bootstrap\cache, and execute the commands:

php artisan cache:clear
php artisan route:cache
php artisan view:clear
php artisan config:cache

Laravel 5.5 LTS*

On my researchs seems there as hack:

https://stackoverflow.com/questions/49443023/requestguardattempt-does-not-exist

And an package where this issue not happen:

https://github.com/spatie/laravel-permission

The hint about to defaults on auth.php got from here:

https://laracasts.com/discuss/channels/laravel/issue-with-auth-receiving-undefined-method

Almost forget, have enabled too on app\Providers\AuthServiceProvider.php:

Passport::enableImplicitGrant();

Please or to participate in this conversation.