How can I use more the one table to attempt login ?
I tried following setting, but it will change to use Admin::class, and the User::class attempt will fail immediately.
app/config
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => FACI\Entities\User::class,
'model' => FACI\Entities\Admin::class,
],
]
this is my attempt
$attempt = \Auth::attempt([
'name' => $this->request->name,
'password' => $this->request->password,
]);
How to told the code to binding the table?
Related
I'm working on a Laravel 8 project that logs a user in through one of two Active Directory servers using LDAP Record 2.5 with a Users table in my DB (MySQL 8) just to have access to some basic info like name, email and a unique user id.
I got the initial part working - I get a successful login to the AD and my users table gets automatically synced with the Active Directory data. If I do a dd() right after the auth()->attempt() I will get what looks like a normally initialized User model and the relevant row in the users table has its remember_token field filled in.
But regardless of whether I pass a remember parameter or not there is no actual authentication - calling auth()->user() or Auth::user() returns a null anywhere except for the login method itself and the debug toolbar doesn't show a user model.
This is the code of my custom LoginController:
public function store(Request $request) {
$credentials = [
'mail' => $request->input('username').'#pio.rs',
'password' => $request->input('password'),
];
$remember = ($request->input('remember') == true) ? true : false;
auth()->shouldUse('pio');
if (auth()->attempt($credentials, $remember)) {
dd(auth()->user()); //returns working user model here
return redirect()->route('home');
} else {
auth()->shouldUse('voj');
if (auth()->attempt($credentials, $remember)) {
return redirect()->route('home');
}
}
}
And these are the settings in my config/auth.php file:
'defaults' => [
'guard' => 'pio',
'passwords' => 'users',
],
'guards' => [
'voj' => [
'driver' => 'session',
'provider' => 'voj',
],
'pio' => [
'driver' => 'session',
'provider' => 'pio',
],
],
'providers' => [
'voj' => [
'driver' => 'ldap',
'model' => App\Ldap\Voj\User::class,
'database' => [
'model' => App\Models\User::class,
'password_column' => false,
'sync_attributes' => [
'email' => 'mail',
'name' => 'cn',
],
'sync_existing' => [
'email' => 'mail',
//'operator' => 'ilike',
],
]
],
'pio' => [
'driver' => 'ldap',
'model' => App\Ldap\Pio\User::class,
'database' => [
'model' => App\Models\User::class,
'password_column' => false,
'sync_attributes' => [
'email' => 'mail',
'name' => 'cn',
],
'sync_existing' => [
'email' => 'mail',
//'operator' => 'ilike',
],
]
],
Any idea what I'm doing wrong?
Found the solution - just needed to call the auth middleware with middleware(['auth:pio,voj']) instead of just middleware(['auth'])
I have created an authentication with JWT in custom table. Authentication works fine, i am able to login and token is generated with success. The problem is not being able to get the information like 'id' etc FROM the generated TOKEN of the custom table.
$token = $request->header('Authorization');
$user = JWTAuth::toUser($token);
return $user;
Returned information are form the 'user' table not from the custom table used for the jwt authenticaiton.
My configration looks like
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'partners_credentials' => [
'driver' => 'eloquent',
'table' => 'partners_credentials',
'model' => App\Models\PartnerCredentials::class,
],
],
Guards
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'partners_credentials' => [
'driver' => 'session',
'provider' => 'partners_credentials',
'hash' => false,
],
],
What i am missing here
Try this way
$user = JWTAuth::user();
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?
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());
}