First time i've asked anything on here before and not the most experienced so please be nice!
I have two tables set up in my DB - Users & Agents.
I have tried multiple ways in which to login both of the users and would ideally like them to be able to login from the same form and do a check on both guards to see if the credentials are valid but for some reason when doing doing attempts on the guards it will only work for the table my default is set to in my auth.php file.
Guards I am attempting:
Auth::guard('web')->attempt($credentials)
Auth::guard('agent')->attempt($credentials)
If I set my default guard in my auth.php file to the web guard its works fine and logs in the matching credentials from the Users table but when trying the agents credentials it shows in my network tab:
Login -
POST 302 found
Agent -
GET 302 found
Login -
Get 200 OK
So it's almost like it acknowledges to credentials are correct and re-directs but then doesnt carry on using the selected guard?
LoginController - DoLogin
Auth.php configeration
in your config/auth.php copy below code :
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'agent' => [
'driver' => 'session',
'provider' => 'agents',
]
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'agents' => [
'driver' => 'eloquent',
'model' => App\Agent::class,
]
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'agents' => [
'provider' => 'agents',
'table' => 'password_resets',
'expire' => 60,
],
],
];
in your Login Controller :
public function login(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|min:6'
]);
if (Auth::guard('agent')->attempt(['email' => $request->email, 'password' => $request->password])) {
return redirect('/dashboard/agent');
}
if(Auth::guard('web')->attempt(['email' => $request->email, 'password' => $request->password])){
return redirect('/dashboard/user');
}
return redirect()->back()->withInput($request->input());
}
Related
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
I have a users table already.
I need to log my user in base on another table babies in my database.
/config/auth.php
I've tried adding a guard
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
------------------------------------------------ Added 👇🏾
'baby' => [
'driver' => 'session',
'provider' => 'babies'
],
],
and a provider
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
------------------------------------------------ Added 👇🏾
'babies' => [
'driver' => 'eloquent',
'model' => App\Baby::class,
],
],
and tried the attempt like this :
$auth = Auth::guard('baby')->attempt([
'email' => strtolower(Request::get('email')),
'password' => Request::get('password'),
'status' => 1
]);
It works ... my $auth returns true
but I have no idea how to access the current Auth::Baby or Auth::Object.
Can someone please give me some directions?
$baby = Auth::guard('baby')->user(); $defaultUser = Auth::guard('web')->user(); or Auth::user(); to retrieve logged in user. $isBabyLogged = Auth::guard('baby')->check(); $isDefaulrUserLogged = Auth::guard('web')->check(); or Auth::check(); - to check if the user is logged in.
I changed the default guard to something like below
auth.php
'defaults' => [
'guard' => 'employee',
'passwords' => 'users',
],
'guards' => [
'employee' => [
'driver' => 'session',
'provider' => 'employees',
],
],
'providers' => [
'employees' => [
'driver' => 'eloquent',
'model' => App\Employee::class,
],
],
However, I can't get the current login user from this Auth facade Auth::guard('employee')->user()
this returns an empty result.
But the user login work successfully Auth::attempt(['email' => $request->email, 'password' => $request->password, 'status' => 1])
I really appreciate your help.
Thanks
Thanks for the viewers. Instead of using Auth::guard('employee')->user() facade, I tried login method $request call. It works for me. $request->user();
public function login(Request $request){
$request->user();
}
I'm trying to make authentication via api in laravel, use custom fields to login and also use custom fields instead of the default Email, I'm not able to do that and I'm quite lost. Don't know if it is even possible.
So far I tried this:
otherusers entity
- idnumber
- password
auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'otherusers',
'hash' => false,
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Domain\Entities\User::class,
],
'otherusers' => [
'driver' => 'eloquent',
'model' => App\Domain\Entities\OtherUser::class,
],
],
And then In the authcontroller I try to use this.
$credentials = [ 'idnumber' => $theidnumber , 'password' => $thepassword ]
Auth::guard('api')->check($credentials);
//Also tried with attempt but it says unknown method.
Obiously I adapted the naming,
any hint?
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';