How to use laravel fortify for authentication in multiple places on the same website
Asked Answered
T

2

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?

Trench answered 18/9, 2020 at 13:52 Comment(0)
B
1

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

Butterfingers answered 21/12, 2020 at 18:25 Comment(0)
T
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;
    }
    
Tracheostomy answered 6/9, 2022 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.