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'));
}
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.
What would be the best way to implement authentication in laravel if I already have my users table (I imported it through an sql dump) and cannot modify its structure?
I don't have any model for this new table. Also the fields in my custom table are different from the default user table from laravel (It doesn't have rememberToken(), createdAt, updatedAt).
These are the changes I made to auth.php:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'database',
'table' => 'users',
],
],
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
});
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?