Laravel 8喷气流烟囱惯性.重定向至"登录后返回主页",而不是"用户选择"



我遇到的情况是,如果用户将url:project.test/med放入,系统会重定向到Login,但不会重定向到dashboard,而是重定向到med。在RedirectIfAuthenticated中,表达式Auth::guard($guard(->check((没有被评估,或者至少在日志中没有显示,所以我无法确定发生了什么。

/** RedirectIfAuthenticated.php */
public function handle($request, Closure $next, ...$guards)
{
$guards = empty($guards) ? [null] : $guards;
   Log::info($request);
   Log::info(Auth::user());
foreach ($guards as $guard) {
     Log::info(Auth::guard($guard)->check());
     Log::info(Auth::check());
if (Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::HOME);
      }
   }
  Log::info(Auth::check());
   Log::info('end');
return $next($request);
 }
/** web.php */
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return InertiaInertia::render('Dashboard');
})->name('dashboard');

Route::middleware(['auth:sanctum','verified'])->get('/med', function (Request $request) {
return InertiaInertia::render('Med');
})->name('med');

转到config/foundation.php并修改此行:

'home' => RouteServiceProvider::HOME,

至:

'home' => function(){
//if you want to go to a specific route
return route('dashboard');

//or if you have a bunch of redirection options
if (Auth::user()->hasRole('admin')) {
return route('admin.dashboard');
}
else{
return route('guest.dashboard');
}
}

EDIT:Fortify现在提供了一种更简单的方法。请参阅此答案以了解简化方法。


RedirectIfAuthenticated中间件不会做您认为它会做的事情。当用户尝试访问连接有guest中间件的路由时,该中间件会检查用户是否通过了身份验证。因此,当您登录到应用程序并尝试访问/login路由后,该中间件将拦截请求并将用户重定向到RouteServiceProvider::HOME路径。


话虽如此,在撰写本文时,Laravel Fortify无法轻松修改用户在注册或登录应用程序后重定向的位置。有关更多信息,请参阅问题

要想让它发挥作用,你必须经历几个步骤。

步骤1-创建新的LoginResponse类

应用程序中的任何位置都可以创建LoginResponse类并实现LaravelFortifyContractsLoginResponse

<?php
namespace AppHttpResponses;
use LaravelFortifyContractsLoginResponse as LoginResponseContract;
class LoginResponse implements LoginResponseContract
{
/**
* @inheritDoc
*/
public function toResponse($request)
{
return $request->wantsJson()
? response()->json(['two_factor' => false])
: redirect()->intended(config('fortify.home')); // This is the line you want to modify so the application behaves the way you want.
}
}

步骤2-覆盖默认的强化登录响应

在这一步中,您必须告诉Laravel使用您的登录响应,而不是默认的Fortify响应。

<?php
namespace AppProviders;
use AppHttpResponsesLoginResponse;
use IlluminateSupportServiceProvider;
use LaravelFortifyContractsLoginResponse as LoginResponseContract;
class FortifyServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(
LoginResponseContract::class, // Contract is required in order to make this step work.
LoginResponse::class,
);
}
//...
}

一旦你完成了更改,用户将被重定向到你设置的任何位置。

有一个更简单的解决方案。

如果您查看config/fortiy.php,您将看到主变量设置为:

'home' => RouteServiceProvider::HOME,

这是指app/Providers/RouteServiceProvider.php中的一个公共常量变量

因此,要在用户登录后将其重定向到主页,只需更改主页变量即可:

public const HOME = '/';

这样做稍微不那么复杂,并且省去了创建一个全新的登录响应类的麻烦。

如果更改没有立即生效,请尝试运行:

php artisan optimize

只需打开配置文件夹中的强化

搜索"主页"=>路由服务提供商::主页

并替换它,但你的代码逻辑,不要忘记添加Auth facade

use IlluminateSupportFacadesAuth;

类似于:

'home' => function () {
$is_active = Auth::user()->is_active;
$role = Auth::user()->role;

if ($is_active == 1) {
if ($role== 1) {
return '/admin/dashboard';   
} else {
return '/';
}
} else {
return '/login';
}
},

最新更新