I want a different Model the be able to login too.
The name of the Model is Client.
So this is added this to auth.php
config:
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'client' => [
'driver' => 'session',
'provider' => 'clients',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => AppModelsUser::class,
],
'clients' => [
'driver' => 'eloquent',
'model' => AppModelsClient::class,
],
],
And expected that this should work:
Auth::guard('client')->loginUsingId($client->id, true);
But this doesn’t show any Authenticated Client:
return Auth::guard('client')->user();
What am I missing?
Routes are like this:
Route::group(['middleware' => ['auth:client']], function () {
Route::get('my-orders', [OrderController::class, 'index']); //goes to login route });
This is the login code:
Auth::guard('client')->loginUsingId($client->id, true);
ray(['Auth' => Auth::guard('client')->user()]); // Shows correct Auth
return redirect()->to("/{$locale}/my-orders"); // Re-redirects to Login route
2
Answers
If you’re using auth in another model you have to use
IlluminateContractsAuthAuthenticatable
interface andIlluminateAuthAuthenticatable
traitExample
then
after that, these will work
You need to use the default auth in Laravel with more than one model and more than one guard
to make a set of steps
Here are some details ..
1 – You must first add more than one guard
from file "config/auth.php"
Indeed, you did this step, so I will not explain it
2 – You must specify guard in model
using the protected $guard = "guard_name"
And use the auth trait
and implement auth interface
3 – You have to make some changes to the
"LoginController" in this place "app/Http/Controllers/Auth"
4 – You should also do some modifications to the
"RegisterController" in this place "app/Http/Controllers/Auth"
5 – You must specify the place where the person who registers will go after registration, based on the gurd, if it is the control panel or the home page
By default it is the home page
6 – You have to modify the middleware
also
From this place
"RedirectIfAuthenticated.php"
7 – You may also need to modify the handler class
From this place
"app/Exceptions/Handler.php"
8 – And of course you have to modify the views
9 – You may also need to modify the route
I think that’s all you need
Here are some references to know what needs to be modified in the code with an example
I hope this helps you, good luck