In my Laravel project I've implemented users' authentication using JWT, socialite and dingo.
Now I have other users which are in another table other than users table -let's call it 'doctors'- and i want to authenticate them.
How to separate the sessions of the two tables even though the configuration for authentication is
//config/auth.php
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
],
i have to edit config/auth to
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'user' => [
'driver' => 'jwt',
'provider' => 'users',
],
'hospitalization' => [
'driver' => 'jwt',
'provider' => 'hospitalizations',
]
],
and use Auth::guard('hospitalization') when i need to separate
Related
Users table was made when
php artisan make:auth
was executed.
I would like to another name table rather than users.
I am trying to put auth info to new_users.
So I tried to change some in my project, making model newUser.php
auth.php
'defaults' => [
'guard' => 'new_users',
'passwords' => ‘new_users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'new_users' => [
'driver' => 'session',
'provider' => 'new_users',
],
'api' => [
'driver' => 'token',
'provider' => 'new_users',
'hash' => false,
],
However,When I click the submit button at [login.blade and register.blade].
the data was not submitted and page is only reloaded.
No exceptions and errors happened
How can I fix it?
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 getting this error:
Call to undefined method Illuminate\Auth\TokenGuard::attempt()
From this code:
if(Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)){
return redirect()->intended(route('admin.dashboard'));
}else{
I have imported Illuminate\Support\Facades\Auth as the docs suggest
My auth.php may help
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'admin-api' => [
'driver' => 'token',
'provider' => 'admins',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 15,
],
],
];
I think you are probably trying to use a "session" guard driver instead "token". So, try to do this:
In config/auth.php configuration file:
'admin-api' => [
'driver' => 'token',
'provider' => 'admins',
],
You have to change it to
'admin-api' => [
'driver' => 'session',
'provider' => 'admins',
],
Then you should run:
php artisan cache:clear
php artisan config:cache
And give it a try again. Good luck!
The token guard doesn't have an attempt method, this is a function used in session authentication. So you will need to authorize the users yourself or use the Laravel Passport authentication https://laravel.com/docs/6.x/passport
auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
/* Foundation-Auth */
AuthenticateUsers.php in laravel->foundation/auth
return $this->guard('admin')->attempt(
$this->credentials($request), $request->has('remember')
);
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.