1

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' => App\Models\User::class,
    ],

    'clients' => [
        'driver' => 'eloquent',
        'model' => App\Models\Client::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
user1469734
  • 851
  • 14
  • 50
  • 81
  • This configuration looks correct, is the user logging in with a form or is this all internal? Is the user authenticated with both guards at once? Does `Auth::user()` show what you'd expect? – miken32 Jun 08 '23 at 15:02
  • @miken32 Authenticated once. Hardcoded via the `loginUsingId()`. The `Auth::user()` is blank. Think I'm missing maybe some `use` things in the Model? So it's known the Model is Authenticatable..? – user1469734 Jun 08 '23 at 15:06
  • 1
    Well you haven't shown us the model code. Did you compare with your `User` model? – miken32 Jun 08 '23 at 15:09
  • Just to confirm, `$client->id` exists in the database right? This won't work with new model instances until you've saved them – apokryfos Jun 08 '23 at 17:03
  • Yes, I exists. @apokryfos - It's an existing user. Only thing is, that I'm using ULID's.. Thats the thing maybe? That's the only changes from a default Model i have – user1469734 Jun 08 '23 at 19:31
  • Since navigating to a new page involves a completely new independent request, I believe the request is treated as a new session. More information is needed to detect the problem, but it is certain that for some reason, when a new HTTP request is made, your previous session ends, and you receive a new one. Is session handling properly configured? (I assume that your LoginController, login route, and Client Model are correct. Unfortunately, you haven't shared them.) – rozsazoltan Jun 14 '23 at 18:05
  • Make sure you have implemented the ULID's properly: https://laravel.com/docs/10.x/eloquent#uuid-and-ulid-keys – Second2None Jun 15 '23 at 00:59
  • @user1469734 did you found the answer? – BlackLotus Jun 17 '23 at 16:49

3 Answers3

0

If you're using auth in another model you have to use Illuminate\Contracts\Auth\Authenticatable interface and Illuminate\Auth\Authenticatable trait

Example

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Model;

class Client extends Model implements AuthenticatableContract
{
    use Authenticatable;

}

then

php artisan config:cache

after that, these will work

Auth::guard('client')->loginUsingId($client->id, true)
Auth::guard('client')->user()

Make sure you've required columns email or username and password and remember_token


Edit 01

In redirectIfAuthenticated(located in app/Http/Middleware/RedirectIfAuthenticated.php)

add this

public function handle($request, Closure $next, ...$guards)
{
    $guards = empty($guards) ? [null] : $guards;

    foreach ($guards as $guard) {
        if (Auth::guard($guard)->check()) {
            switch ($guard) {
                case 'client':
                    return redirect(route('client.dashboard'));
                default:
                    return redirect(RouteServiceProvider::HOME);
            }
        }
    }

    return $next($request);
}

Updating middleware to be aware of the new guard.

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • Hi, Thanks! Tried that, but keeps redirecting me to the default login-route. `Route::group(['middleware' => ['auth:client']], function () { Route::get('my-orders', [OrderController::class, 'index']); // goes to login route });` – user1469734 Jun 09 '23 at 10:12
  • `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` – user1469734 Jun 09 '23 at 10:16
  • @user1469734 use the above code structure by removing all other what you tried and add **Edit 01** as well – Abdulla Nilam Jun 15 '23 at 06:49
-1

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

https://techvblogs.com/blog/multiple-authentication-guards-laravel-9

Dharman
  • 30,962
  • 25
  • 85
  • 135
mohammed albadry
  • 135
  • 1
  • 14
-1

There might be a few reasons why is NOT authenticating using the client guard:

  • The request is not authenticated at all. This could be because the user has not logged in, or because the request is using an unauthorized method, such as GET.
  • The request is authenticated using a different guard. This could be because the user is logged in using a different guard, such as web.

So, verify that the current request is authenticated using the client guard, with this method:

auth()->guard('client')->check()

Is the user Authenticated? Returns:

true (1) OR false (0)

To force it use:

$user = User::find(1);

if (auth()->guard('client')->login($user)) {
// The user is now logged in using the `client` guard.
} else {
// The login failed.
}

This would give you a good idea of why is failing. I hope it helps!