简体   繁体   中英

Laravel 8.8 redirectTo function does't work

I need to implement logic when authorizing a user and I planned to do this in the redirectTo() function in the LoginController . However, Laravel does not seem to see it and always redirects to the HOME constant.

It seems that all files are correct.

LoginController

protected function redirectTo()
{

    return redirect('/test');
}

auth-backend/RedirectsUsers

public function handle(Request $request, Closure $next, ...$guards)
{
    $guards = empty($guards) ? [null] : $guards;

    foreach ($guards as $guard) {
        if (Auth::guard($guard)->check()) {
            return redirect('/account');
        }
    }

    return $next($request);
}

Some users have found that the redirect happens in the middleware/RedirectIfAuthenticated , but I don't know how to fix it. If in this file I replace return redirect(RouteServiceProvider::HOME) with some path, for example redirect('/account') then it returns /account . But I still need to be in the redirectTo() function.

Middleware/RedirectIfAuthenticated

public function handle(Request $request, Closure $next, ...$guards)
{
    $guards = empty($guards) ? [null] : $guards;

    foreach ($guards as $guard) {
        if (Auth::guard($guard)->check()) {
            return redirect('/account');
        }
    }

    return $next($request);
}

How can I make the process come to redirectTo() after authorization?

  1. Define a route in web.php for redirectTo function in Login Controller

    Route::get('check-auth', [LoginController::class, 'redirectTo'])->name('auth.redirect-to');

  2. return to the route

    return redirect()->route('auth.redirect-to');

make sure to use the LoginController on the top of web.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM