How did you register this middleware?
Redirect after login according to user role laravel fortify
I want to redirect on different URLs after login. I am using laravel fortify. I am always redirecting on URL which is defined in config\fortify.php
'home' => RouteServiceProvider::HOME,
// or
'home' => '/dashboard'
but it must be a conditional redirect. I tried to redirect by using a middleware but not redirecting.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class Sellers
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (!Auth::check()) {
return redirect()->route('login');
}
elseif(in_array(Auth::user()->role, ['grocery', 'restaurant', 'pharmacy', 'delivery', 'vegetables'])) {
return redirect('dashboard');
}
return $next($request);
}
}
Finally, I tried according to laravel official documentation but also failed. https://laravel.com/docs/8.x/authentication#redirecting-unauthenticated-users
Please help me, thanks in advance.
And how are you associating this route with your controller?
As you are using Fortify, please read this blog post from @snapey :
https://talltips.novate.co.uk/laravel/laravel-8-conditional-login-redirects
It uses a different approach that suits Fortify better.
Please or to participate in this conversation.