5

发生在我身上的是,如果用户输入 url:project.test/med,系统会重定向到 Login,但它不会重定向到仪表板,而是重定向到 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 Inertia\Inertia::render('Dashboard');
    })->name('dashboard');
    
    Route::middleware(['auth:sanctum','verified'])->get('/med', function (Request $request) {
         return Inertia\Inertia::render('Med');
    })->name('med');
4

4 回答 4

11

转到config/fortify.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');
    }
}
于 2021-02-20T13:58:29.750 回答
7

中间件并没有按照RedirectIfAuthenticated你的想法去做。该中间件检查用户在尝试访问带有guest中间件的路由时是否经过身份验证。因此,在您登录应用程序并尝试访问/login路由后,此中间件将拦截请求并将用户重定向到RouteServiceProvider::HOME路径。


话虽如此,在撰写本文时,Laravel Fortify 还没有提供轻松修改用户在注册或登录到应用程序后重定向到哪里的能力。有关更多信息,请参阅问题

要使其正常工作,您必须执行几个步骤。

第 1 步 - 创建新的 LoginResponse 类

在您的应用程序中的任何地方创建一个LoginResponse类并实现Laravel\Fortify\Contracts\LoginResponse.

<?php

namespace App\Http\Responses;

use Laravel\Fortify\Contracts\LoginResponse 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 步 - 覆盖默认的 Fortify 登录响应

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

<?php

namespace App\Providers;

use App\Http\Responses\LoginResponse;
use Illuminate\Support\ServiceProvider;
use Laravel\Fortify\Contracts\LoginResponse 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,
        );
    }

    //...
}

完成更改后,用户将被重定向到您设置的任何位置。

于 2020-10-02T16:20:40.873 回答
5

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

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

'home' => RouteServiceProvider::HOME,

这指的是公共 const 变量app/Providers/RouteServiceProvider.php

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

public const HOME = '/';

这样做会稍微简单一些,并且不必创建一个全新的登录响应类。

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

php artisan optimize
于 2020-12-27T01:25:43.973 回答
2

只需在配置文件夹中打开 fortify

搜索“家”=> RouteServiceProvider::HOME

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

use Illuminate\Support\Facades\Auth;

就像是:

'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';
    }
},
于 2021-03-09T14:15:43.110 回答