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 IlluminateAuthRequestGuard::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.
| 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
Tell us how to reproduce this issue.
Tell us what should happen
Tell us what happens instead
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.
same for me, i get same error. how to fix this?
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.
any updates? i have this problem too
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,
]
],
];
Solved...
@sanjukaniyamattam make sure that your are using default guard web on config/auth.php
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
If you look at the documentation you will see that:
config/auth.php
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
for
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
any news? same error here when i useguard => 'api' =/
Was this resolved? It's been over a year, I'm having the same issue
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 :)
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
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!
Hey Guys, i am using laravel/lumen-framework": "^7.0" and i am still facing "Method IlluminateAuthRequestGuard::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?
@Egzonrexhepi
there is no IlluminateAuthRequestGuard as you mention, or maybe I don't know about,
there is : Illuminate\Support\FacadesAuth
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.
@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.
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
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
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 hereWhat I have to do if I want to use another guard ? like guard('admin'). I've already creted this guard
check my answer
@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?
Do Like This..............
Setp 1 : First Create Table .
Step 2 : Create Model.
Step 3 : Create Guard in Auth.php
'guards' => [
'delivery' => [
'driver' => 'session',
'provider' => 'delivery_boy',
],
],
'providers' => [
'delivery_boy'=> [
'driver' => 'eloquent',
'model' => App\Model\Auth\Delivery::class,
],
],
namespace App\ModelAuth;
use Illuminate\ContractsAuth\MustVerifyEmail;
use Illuminate\FoundationAuth\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
*/
}
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.
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
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.
Most helpful comment
Solved...
@sanjukaniyamattam make sure that your are using default guard
webonconfig/auth.php