How to Authenticate users with specific table other than users table? - php

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.

Related

Laravel , how to get loggedin user from jwt token of a custom table

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();

How to Implement Authentication Guard without Authentication Defaults

Authentication Defaults
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
Authentication Guards
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
'hash' => false,
],
'student' => [
'driver' => 'session',
'provider' => 'students',
]
],
User Providers
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'students' => [
'driver' => 'eloquent',
'model' => App\Student::class,
]
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
when i try to login using student table username, password its not login but when i change the
Auth Defaults to students
'defaults' => [
'guard' => 'api',
'passwords' => 'students',
],
and api to students provider
'api' => [
'driver' => 'jwt',
'provider' => 'users',
'hash' => false,
],
its login using students table data but the users table datas not allow to login.
How can i implement this two users table to login??
thanks in advance
you should pass guard name to auth function :
auth('students')->login();
or
Auth::guard('students')->login()
or you override default guard by passing there to auth middleware:
Route::group(['middleware' => ['auth:students'] ], function(){
//your routes here
});

Authentication with Laravel 6 and passport Use Custom fields and entity

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?

How to defined multiple guard and access to it's class member in from laravel4.2 to Laravel5.3.19

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 am applying multi auth in laravel 5.2 but authentication is not working with signle login Form

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';

Categories