tymondesigns / jwt-auth

🔐 JSON Web Token Authentication for Laravel & Lumen
https://jwt-auth.com
MIT License
11.29k stars 1.54k forks source link

Method Illuminate\Auth\RequestGuard::attempt does not exist #1498

Open sanjukaniyamattam opened 6 years ago

sanjukaniyamattam commented 6 years ago

Method Illuminate\Auth\RequestGuard::attempt does not exist

I am new to both laravel and lumen. I was creating a login api with oauth2.0, i have installed passport and generated token. Below is my login controller function and it is working fine. It returns token.

public function login(Request $request) { global $app;
$proxy = Request::create( '/oauth/token', 'post', [ 'grant_type' => env('API_GRAND_TYPE'), 'client_id' => env('API_CLIENT_ID'), 'client_secret' => env('API_CLIENT_SECRET'), 'username' => $request->username, 'password' => $request->password, ]

        );
        return $app->dispatch($proxy);
    }  

Since i have to check user status apart from username and password, i need to check the user credential first. so i do like this.

public function login(Request $request) {

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

if (Auth::attempt($credentials)) {
    return ['result' => 'ok'];
}

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

}

Here i am getting this error. Method Illuminate\Auth\RequestGuard::attempt does not exist.

So i tried Auth::check instead of Auth::attempt. Now there is no error but it always return false even though the credentials are valid.

I searched a lot for a solution but i didn't get.

Your environment

Q A
Bug? yes
New Feature? no
Framework Laravel / Lumen
Framework version 5.6.0
Package version 1.x.y
PHP version 7.x.y

Steps to reproduce

Tell us how to reproduce this issue.

Expected behaviour

Tell us what should happen

Actual behaviour

Tell us what happens instead

lucastoneatto commented 6 years ago

I am testing with laravel 5.5. and I get the same error, I do not know what it can be. Until a few days ago it worked well for me.

MarkVilludo commented 6 years ago

same for me, i get same error. how to fix this?

ce-page commented 6 years ago

Any update on this? I'm also having this issue. Although I am using the Passport package which allows the actingAs method to be used in my case.

saeedvz commented 6 years ago

any updates? i have this problem too

jcharcosset commented 6 years ago

You should create config/auth.php file to root :

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */
    'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
    ],

    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    */
    'guards' => [
        'api' => [
            'driver' => 'jwt',
            'provider' => 'users',
        ],
    ],

    // optional, if use eloquent
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model'  =>  App\User::class,
        ]
    ],
];
saeedvz commented 6 years ago

Solved... @sanjukaniyamattam make sure that your are using default guard web on config/auth.php

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],
rcfoficial commented 6 years ago

If you look at the documentation you will see that:
config/auth.php

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

for

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

garbinmarcelo commented 5 years ago

any news? same error here when i useguard => 'api' =/

jcalosor commented 5 years ago

Was this resolved? It's been over a year, I'm having the same issue

ixperiencenl commented 5 years ago

If anyone experiences this, please also make sure your middleware route uses the correct provider. My case I had the provider 'customers' and in my middleware route I set ['middleware' => ['jwt.auth:customer']] which obviously was a typo. Changing the provider to customer fixed this issue at least :)

Elshaden commented 5 years ago

for any one that comes across this post The function ( Auth::attempt ) this is only available for routes with web guard so if you are using another middleware to access this function you need to use the web guard. A simple solution is to insert guard('web') , Auth::guard('web')->attempt like so

$credentials = ['email' => $request->username, 'password' => $request->password];

if (Auth::guard('web')->attempt($credentials, false, false)) {
    return ['result' => 'ok'];
}

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

This should give you access to the Auth class without using the web guard in your request.

solution taken from here

frknasir commented 4 years ago

for any one that comes across this post The function ( Auth::attempt ) this is only available for routes with web guard so if you are using another middleware to access this function you need to use the web guard. A simple solution is to insert guard('web') , Auth::guard('web')->attempt like so

$credentials = ['email' => $request->username, 'password' => $request->password];

if (Auth::guard('web')->attempt($credentials, false, false)) {
    return ['result' => 'ok'];
}

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

This should give you access to the Auth class without using the web guard in your request.

solution taken from here

Awesome!

Egzonrexhepi commented 4 years ago

Hey Guys, i am using laravel/lumen-framework": "^7.0" and i am still facing "Method Illuminate\Auth\RequestGuard::attempt does not exist." when i try Auth::attempt($credentials), i have followed all the tips and hints above but still not sure why i am getting this, anyone else found any other fix?

Elshaden commented 4 years ago

@Egzonrexhepi there is no Illuminate\Auth\RequestGuard as you mention, or maybe I don't know about, there is : Illuminate\Support\Facades\Auth

and you can use it in Auth::attempt($credentials), if you use 'web ' middleware , this should work, if not try use Auth::guard('web') ->attempt($credentials)

hope this work.

Egzonrexhepi commented 4 years ago

@Elshaden Thanks for reply, actually i forgot to register routes, and made some mistakes in the whole passport implementation, i got it working after registering routes.

baselbj commented 4 years ago

I think it it is confusing how Laravel implements the guards. There is a Guard interface (Illuminate\Contracts\Auth\Guard) that you should implement in all your guards, this is what you get when you use the Auth::viaRequest method.

At the same time the session guard implements more methods than the once in the Guard interface and those methods are the once they are using in the documentation like the attempt method.

To solve this issue you should create a custom guard, using (Auth::extend) not using (Auth::viaRequest) you can use the methods in the Illuminate\Auth\SessionGuard and add any one you need. Later you can call them using Auth::yourMethod.

Summary: you can create your own Guard with any extra methods you need like the attempt

Emanuellvr commented 4 years ago

for any one that comes across this post The function ( Auth::attempt ) this is only available for routes with web guard so if you are using another middleware to access this function you need to use the web guard. A simple solution is to insert guard('web') , Auth::guard('web')->attempt like so

$credentials = ['email' => $request->username, 'password' => $request->password];

if (Auth::guard('web')->attempt($credentials, false, false)) {
    return ['result' => 'ok'];
}

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

This should give you access to the Auth class without using the web guard in your request.

solution taken from here

What I have to do if I want to use another guard ? like guard('admin'). I've already creted this guard

baselbj commented 4 years ago

for any one that comes across this post The function ( Auth::attempt ) this is only available for routes with web guard so if you are using another middleware to access this function you need to use the web guard. A simple solution is to insert guard('web') , Auth::guard('web')->attempt like so

$credentials = ['email' => $request->username, 'password' => $request->password];

if (Auth::guard('web')->attempt($credentials, false, false)) {
    return ['result' => 'ok'];
}

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

This should give you access to the Auth class without using the web guard in your request. solution taken from here

What I have to do if I want to use another guard ? like guard('admin'). I've already creted this guard

check my answer

ermmak commented 4 years ago

@Elshaden Thanks for reply, actually i forgot to register routes, and made some mistakes in the whole passport implementation, i got it working after registering routes.

Could you give us more detailed explanation of your solution please?

lokey1410 commented 4 years ago

Do Like This.............. Setp 1 : First Create Table . Step 2 : Create Model. Step 3 : Create Guard in Auth.php

in Auth

'guards' => [

    'delivery' => [
        'driver' => 'session',
        'provider' => 'delivery_boy',
    ],
],

mention "provider " same like db table name "delivery_boy"

'providers' => [
    'delivery_boy'=> [
    'driver' => 'eloquent',
    'model' => App\Model\Auth\Delivery::class,
],
],

Then modify in your model

namespace App\Model\Auth; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Laravel\Passport\HasApiTokens;

class Delivery extends Authenticatable { use HasApiTokens,Notifiable;

protected $guard ='delivery';
protected $table ='delivery_boy';
protected $primarykey ='id';

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

protected $fillable = [
     'email', 'password','mobile',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password',
];

/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */

}

stale[bot] commented 3 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

guma256 commented 3 years ago

I had the same problem with Lumen, I created folder config and created auth.php file inside config folder and added the following code to auth.php


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

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

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

This solved my problem thank you

fsevenm commented 3 years ago

for any one that comes across this post The function ( Auth::attempt ) this is only available for routes with web guard so if you are using another middleware to access this function you need to use the web guard. A simple solution is to insert guard('web') , Auth::guard('web')->attempt like so

$credentials = ['email' => $request->username, 'password' => $request->password];

if (Auth::guard('web')->attempt($credentials, false, false)) {
    return ['result' => 'ok'];
}

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

This should give you access to the Auth class without using the web guard in your request.

solution taken from here

The only solution that works for me. With sanctum tho.