I am using Laravel 8 and I was trying to make a guard 'mobile'. I followed some tutorials online but I don't know why what seems to be working in those tutorials is not working at my end.
I searched on internet and all I could find was php artisan config:clear and it never worked for me.
this is code from my auth.php file:
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'mobile'=> [
'driver'=>'session',
'provider'=>'technicians'
],
'api' => [
'driver' => 'sanctum',
'provider' => 'users',
'hash' => false,
]
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'technicians' => [
'driver' => 'eloquent',
'model' => App\Models\Technician::class,
]
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
'technicians' => [
'provider' => 'technicians',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60
]
],
'password_timeout' => 10800,
];
In my controllers I am using this line to authenticate, which in return is giving me the error:
Auth::guard('mobile')->attempt(['email'=>'testmail#test.com',password=>'password']);
I found the source of error. I was using phpstorm to automatically upload files through ftp to the server. So, I accidentally had config folder excluded from the list of folders being synced. so my auth.php was never actually updtating.
Otherwise code is fine
Related
I made changes in auth file but still, I am getting an error.
Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must
be an instance of Illuminate\Contracts\Auth\UserProvider, null given,
called in
C:\LMS\vendor\laravel\framework\src\Illuminate\Auth\AuthManager.php on
line 123
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'admin1' => [
'driver' => 'eloquent',
'model' => App\RegisterOperation::class,
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'user' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin1',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];
correct your guard, you have something missing under your providers
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'user' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin1',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admin1' => [
'driver' => 'eloquent',
'model' => App\RegisterOperation::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];
Use this package It you give the best support for multiple role based user
https://github.com/Zizaco/entrust
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,
],
],
];
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!
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
I'm getting the error:
Auth guard [accountant] is not defined.
This is my config\auth.php file
<?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',
],
'accountant' => [
'driver' => 'session',
'provider' => 'accountants',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
'accountants' => [
'driver' => 'eloquent',
'model' => App\Accountant::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 60,
],
'accountants' => [
'provider' => 'accountants',
'table' => 'password_resets',
'expire' => 60,
],
],
];
The previous guard admin is working fine. But the accountant guard is throwing an error. What have I done incorrectly?
I have added the guard in the model as well
You may need to clear the config cache:
php artisan config:clear
Here are the docs on configuration caching: https://laravel.com/docs/5.4/configuration#configuration-caching
You may have previously run the config:cache command on your dev instance
I'm just upgrade From laravel4.2 to laravel5.3.19 now i want to access Authentication data by below method
1: Auth::employees()->attempt($data)
2: Auth::admin()->logout()
3: Auth::admin()->get()->email
.................................
Example: Auth::guard->guardName->property.
I think All of those method should be call
1:Auth::user()->id
But i don't want to correct all old method because they used a lot of in this system so i want to find the solution by keeping those method as the same. And I have configured Auth.php in Configure directory as below but it simply not work as expected.
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin'
],
'employee' => [
'driver' => 'session',
'provider' => 'employee'
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'employee'=>[
'driver'=>'eloquent',
'model'=>App\Models\Employee::class
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Models\Admin::class
],
],
Please help.
i have change my code according to the given link
change my code according the link instruction
but they used two login form i want to use single login form for authenticate two
users table is there any idea. kindly help
auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'company' =>[
'driver'=>'session',
'provider' => 'company'
]
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
'company' =>[
'driver' => 'eloquent',
'model' => 'App\Http\Models\company::class',
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
'companies'=>[
'provider' => 'company',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
]
],
i copy default auth two files to the new companyauth .
and define
protected $redirectTo = 'company-home';
protected $guard = 'company';