Beans
119
2
Laravel

Auth guard only working for default

Posted 1 month ago by Beans

I am new to laravel so please bear with me. I am trying to implement multi authentication using one login form but I am running into some trouble as the auth guard only works for the default in the auth.php file. I've tried a bunch of solutions here but they didn't seem to work. What can I do?

My auth.php file


    return [

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],

        'employee' => [
            'driver' => 'session',
            'provider' => 'employees',
        ],
    ],

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],

    
        'employees' => [
            'driver' => 'eloquent',
            'model' => App\Employee::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

   

    

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'employees' => [
            'provider' => 'employees',
            'table' => 'password_resets',
            'expire' => 60,
        ],
     ],

    ]; 

My LoginController

 <?php

    namespace App\Http\Controllers\Auth;

    use App\Http\Controllers\Controller;
    use Illuminate\Foundation\Auth\AuthenticatesUsers;

    class LoginController extends Controller
    {

    use AuthenticatesUsers;

   
    protected $redirectTo = '/dashboard';

    

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
      }
    }

my AttemptLogin function inside the AuthenticatesUsers trait


    protected function attemptLogin(Request $request)
    {
       
        $userAttempt = $this->guard()->attempt(
                $this->credentials($request), $request->filled('remember')
        );
        if(!$userAttempt){
            $this->checkUser = "employee";
            return $this->employeeGuard()->attempt(
                $this->credentials($request), $request->filled('remember')
        );
           
        }
        return $userAttempt;
        
    }

My model

    <?php

    namespace App;


    use Illuminate\Notifications\Notifiable;
    use Illuminate\Contracts\Auth\MustVerifyEmail;
    use Illuminate\Foundation\Auth\User as Authenticatable;



    class Employee extends Authenticatable
    {
    use Notifiable;

        protected $guard = "employees";

        
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'firstname', 'surname', 'DOB', 'gender', 'mobile_number', 
    'job_title','job_sector', 
        'highest_qualification', 'experience', 'short_description', 
    'relocation_willingness', 
        'salary_range', 'city', 'cv', 'five_tag_summation', 'email', 
    'password',
    ];

    
    protected $hidden = [
        'password', 'remember_token',
    ];

    
 
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    }

Please sign in or create an account to participate in this conversation.