MrFiliper's avatar

Lumen undefined method RequestGuard::attempt() in auth

Hello, I need it to create api for user authentication in Lumen, but there are some problems.

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

in AuthManager.php (line 294)
at AuthManager->__call('attempt', array(array('username' => 'test', 'password' => 'test'), false))
in Facade.php (line 221)
at Facade::__callStatic('attempt', array(array('username' => 'test', 'password' => 'test'), false))
in AuthController.php (line 29)
at AuthController->login(object(Request))
at call_user_func_array(array(object(AuthController), 'login'), array(object(Request)))
in BoundMethod.php (line 29)
at BoundMethod::Illuminate\Container\{closure}()
in BoundMethod.php (line 87)
at BoundMethod::callBoundMethod(object(Application), array(object(AuthController), 'login'), object(Closure))
in BoundMethod.php (line 31)
at BoundMethod::call(object(Application), array(object(AuthController), 'login'), array(), null)
in Container.php (line 531)
at Container->call(array(object(AuthController), 'login'), array())
in RoutesRequests.php (line 741)
at Application->callControllerCallable(array(object(AuthController), 'login'), array())
in RoutesRequests.php (line 707)
at Application->callLumenController(object(AuthController), 'login', array(true, array('uses' => 'App\\Http\\Controllers\\AuthController@login'), array()))
in RoutesRequests.php (line 681)
at Application->callControllerAction(array(true, array('uses' => 'App\\Http\\Controllers\\AuthController@login'), array()))
in RoutesRequests.php (line 643)
at Application->callActionOnArrayBasedRoute(array(true, array('uses' => 'App\\Http\\Controllers\\AuthController@login'), array()))
in RoutesRequests.php (line 628)
at Application->handleFoundRoute(array(true, array('uses' => 'App\\Http\\Controllers\\AuthController@login'), array()))
in RoutesRequests.php (line 528)
at Application->Laravel\Lumen\Concerns\{closure}()
in RoutesRequests.php (line 781)
at Application->sendThroughPipeline(array(), object(Closure))
in RoutesRequests.php (line 534)
at Application->dispatch(null)
in RoutesRequests.php (line 475)
at Application->run()
in index.php (line 28)

AuthController

class AuthController extends Controller
{

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

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

        Auth::attempt($credentials, false); //ERROR

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

User

class User extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable;

    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */

    protected $fillable = [
        'username', 'email',
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password',
    ];
}

I don't know where is problem, why is attempt() method not found?

0 likes
2 replies
AddWebContribution's avatar

I think you should update your config/auth.php file

'user' => [
    'driver' => 'token',
    'provider' => 'userProvider',
],

to change

'user' => [
    'driver' => 'session',
    'provider' => 'userProvider',
],

Hope this solve your issue !

MrFiliper's avatar

My auth.php

return [
    'defaults' => [
        'guard' => env('AUTH_GUARD', 'api'),
        'passwords' => 'users',
    ],

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

    'user' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

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

First problem is solved? But other one is there.

Class session.store does not exist

in Container.php (line 719)
at ReflectionClass->__construct('session.store')
in Container.php (line 719)
at Container->build('session.store')
in Container.php (line 598)
at Container->resolve('session.store')
in Container.php (line 567)
at Container->make('session.store')
in Application.php (line 208)
at Application->make('session.store')
in Container.php (line 1139)
at Container->offsetGet('session.store')
in AuthManager.php (line 125)
at AuthManager->createSessionDriver('api', array('driver' => 'session', 'provider' => 'users'))
in AuthManager.php (line 96)
at AuthManager->resolve('api')
in AuthManager.php (line 70)
at AuthManager->guard()
in AuthManager.php (line 294)
at AuthManager->__call('attempt', array(array('username' => 'test', 'password' => 'test'), false))
in Facade.php (line 221)
at Facade::__callStatic('attempt', array(array('username' => 'test', 'password' => 'test'), false))
in AuthController.php (line 26)
at AuthController->login(object(Request))
at call_user_func_array(array(object(AuthController), 'login'), array(object(Request)))
in BoundMethod.php (line 29)
at BoundMethod::Illuminate\Container\{closure}()
in BoundMethod.php (line 87)
at BoundMethod::callBoundMethod(object(Application), array(object(AuthController), 'login'), object(Closure))
in BoundMethod.php (line 31)
at BoundMethod::call(object(Application), array(object(AuthController), 'login'), array(), null)
in Container.php (line 531)
at Container->call(array(object(AuthController), 'login'), array())
in RoutesRequests.php (line 741)
at Application->callControllerCallable(array(object(AuthController), 'login'), array())
in RoutesRequests.php (line 707)
at Application->callLumenController(object(AuthController), 'login', array(true, array('uses' => 'App\\Http\\Controllers\\AuthController@login'), array()))
in RoutesRequests.php (line 681)
at Application->callControllerAction(array(true, array('uses' => 'App\\Http\\Controllers\\AuthController@login'), array()))
in RoutesRequests.php (line 643)
at Application->callActionOnArrayBasedRoute(array(true, array('uses' => 'App\\Http\\Controllers\\AuthController@login'), array()))
in RoutesRequests.php (line 628)
at Application->handleFoundRoute(array(true, array('uses' => 'App\\Http\\Controllers\\AuthController@login'), array()))
in RoutesRequests.php (line 528)
at Application->Laravel\Lumen\Concerns\{closure}()
in RoutesRequests.php (line 781)
at Application->sendThroughPipeline(array(), object(Closure))
in RoutesRequests.php (line 534)
at Application->dispatch(null)
in RoutesRequests.php (line 475)
at Application->run()
in index.php (line 28)

Please or to participate in this conversation.