skip to Main Content

I am using laravel 10. I have 2 guards. One is admin and another is manager . The Auth only authenticate the default one so in the controller construction I have set

 public function __construct(){
        Config::set('auth.defaults.guard','admin');
    }

 public function __construct(){
        Config::set('auth.defaults.guard','manager');
    }

Now I have to check the user to verify if it’s admin or manager guard but if I check the guard name its says null. auth()->guard('guard_name')->user()->name; I dont know how can I get the guard name to authenticate!

2

Answers


  1. Chosen as BEST ANSWER

    I have found an idea to solve this issue. When the query successfully executed, setting another value as admin or manager based on their controller and passing the data as array with token.


  2. Instead of setting the guard in the constructor you can define directly the guard when authenticating :

    Auth::guard('guard_name')->attempt(...);
    
    //or
    
    Auth::guard('guard_name')->login($user);
    

    Then if you want to check if the user is authenticated with specific guard you can use the check() method in Auth Facade.

    Auth::guard('guard_name')->check(); // it will return true if there is authenticated user using the given auth guard.
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search