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
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:
- Siteusers Login Route Name : siteusers.login
- Staffusers Login Route Name : staffusers.login
- Name of Auth Guard for Siteusers: siteusers
You can replace these values with actual ones