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?
Related
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 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 am working with a existing laravel project. So on that project the previous one install two auth folder, one for admin and clients. Now my role is adding a seller part. I create separated model and controller for seller part, but when i try to login with seller email and password by any how system search seller email into client database table.
This code for my seller login:
$credentials = $request->only('seller_email','seller_password');
if (Auth::attempt($credentials)){
return redirect()->intended(route('seller.dashboard'));
}
return redirect()->intended(route('seller.login'));
But it searching into the clients table Why?
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'seller_email'
in 'where clause' (SQL: select * from clients where seller_email =
dr#seller.com limit 1);
Config/Auth.php file looks like
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'clients',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'api' => [
'driver' => 'jwt',
'provider' => 'clients',
],
'seller' => [
'driver' => 'session',
'provider' => 'sellers',
]
],
'providers' => [
'clients' => [
'driver' => 'eloquent',
'model' => App\Models\Client::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Models\Admin::class,
],
'sellers' => [
'driver' => 'eloquent',
'model' => App\Seller::class,
]
],
use seller guard while attempting to login as
if (Auth::guard('seller')->attempt($credentials)){
return redirect()->intended(route('seller.dashboard'));
}
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());
}
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.