Multiple URL Redirection in Laravel

I examined many of these issues, they do not fit the real problem. I would like to redirect the user to a specific URL immediately after logging in, depending on the state of the user.

I know this can be archived by middleware, so I tried this in the \ Http \ Middleware \ RedirectIfAuthenticated.php application

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::User()->check()) {
            $redirect = '/client';
            if (Auth::user()->hasRole('admin')){
                $redirect = '/admin';
            }
            return redirect($redirect);
        }
        return $next($request);
    }
}

I understand that this will not work right after logging in. I would like to redirect the user depending on whether he is an administrator or a client. I know I can use: protected $ redirectPath = '/ url / to / redirect'; but I have some pages to redirect.

What is the best way to do this?

+4
1

, /Http/Controllers/Auth/AuthController.php

public function redirectPath()
{
    if (Auth::user()->hasRole('admin')){
        return '/admin';
    }

    return '/client';
}

AuthController.php.

+2

All Articles