6

When using laravel/UI we can create authentication scaffold on multiple places or for multiple users like admin, normal users by copy-pasting the same scaffold generated by the &--auth flag.

How can we achieve the same with Fortify?

How can we use Fortify to authorize admin and normal users?

Ajith Lal
  • 83
  • 2
  • 9

2 Answers2

1

See the link below. You need to use config function.
https://max-eckel.dev/posts/multi-guard-authentication-with-laravel-fortify

Radomír
  • 21
  • 5
0

In order to do multiauth, we have to use multiple guards. But laravel fortify only supports one guard (as of sep-2022) in the config/fortify.php file. So what we need to do:

  1. We need to use authenticateUsing method and return our specific authenticating model (User/Admin/whatever) in the boot method of FortifyServicePorvider:

    Fortify::authenticateUsing(function (Request $request) {
    // check based on the type request:
    switch($request->type){
        case'employee':
            // return employee model all the checks;
        break;
    
        case 'admin':
            // return admin model after all the checks;
        break;
    }
    });
    
  2. Check the same request parameter in the register method of FortifyServicePorvider and set the guard (make sure to create a guard in the config/auth.php file) to use, using global config help:

    switch($request->type){
    case'employee':
        config(['fortify.guard' => 'employee']);
        break;
    
    case 'admin':
        config(['fortify.guard' => 'admin']);
        break;
    }
    
MR_AMDEV
  • 1,712
  • 2
  • 21
  • 38