Home > Net >  How to define redirect path for unauthenticated users of custom auth guard in Laravel8 using Jetstre
How to define redirect path for unauthenticated users of custom auth guard in Laravel8 using Jetstre

Time:01-01

I have defined two different auth guards called 'siteusers' and 'staffusers' in my Laravel 8 based portal in which I am using Jetstream and Fortify for session based authentication.

Now, I have two different URL structure for both type of users

  1. www.sitename.com/siteusers/dashboard
  2. www.sitename.com/staffusers/dashboard

I would like to set an algorithm that if someone clicks on 1st link from above (while not logged in) then he should be redirected to

If someone clicks on 2nd link (while not logged in) then he should be redirected to

Is it possible in Fortify's current version?

CodePudding user response:

Go to app\http\middleware\Authenticate.php

Add Following before starting of the class

use Illuminate\Support\Facades\Route;

Then replace everyting inside the redirectTo function with following code

    $routeMiddleware = Route::current()->middleware();
    // Example Output : Array ( [0] => web [1] => auth:siteusers [2] => verified )
    $route = explode(":", $routeMiddleware[1]);
    $routeName = $route[1];
    if (!$request->expectsJson()) {
        if ($routeName == 'siteusers')
            return route('siteusers.login');
        else
            return route('staffusers.login');
    }

Assumptions Made:

  1. Siteusers Login Route Name : siteusers.login
  2. Staffusers Login Route Name : staffusers.login
  3. Name of Auth Guard for Siteusers: siteusers

You can replace these values with actual ones

  • Related