【问题标题】:Laravel 8 Fortify Redirect based on roles without jetstreamLaravel 8 Fortify Redirect 基于角色而没有 jetstream
【发布时间】:2021-08-27 21:54:58
【问题描述】:

我正在寻找一个很好的教程或建议,关于如何在基于 laravel 8 fortify 中没有 jetstream 的角色进行身份验证后重定向。我找到了一个说要在 App\Http\Responses\loginresponse.php 中创建一个新的 LoginResonse.php,但我不知道在哪里注册这个新响应,因为我使用的教程说要在 Jetstreamserviceprovider 中进行,但是我没有使用 Jetstream。有什么想法吗?

【问题讨论】:

    标签: laravel laravel-fortify


    【解决方案1】:

    您需要在FortifyServiceProvider 中注册您的自定义LoginResponse,特别是boot 方法。

    public function boot()
    {
        $this->app->singleton(
            \Laravel\Fortify\Contracts\LoginResponse::class,
            \App\Http\Responses\LoginResponse::class
        );
    }
    

    这会将您的自定义 LoginResponse 注册到 Laravel IoC 服务容器,并通知 Laravel 将您的 LoginResponse 提供给需要从 Fortify 实现 LoginResponse 的对象实例的方法。

    在您的自定义LoginResponsetoResponse 方法中,您可以执行您需要的任何逻辑。例如;

    public function toResponse($request)
    {
        if ($request->wantsJson()) {
            return response()->json(['two_factor' => false]);
        }
    
        if (Auth::user()->hasRole('admin') {
            return redirect(route('admin.index'));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-13
      • 1970-01-01
      • 2021-12-05
      • 2021-12-11
      • 2021-07-14
      • 2021-11-12
      • 2021-01-26
      • 2022-01-21
      • 2021-01-16
      相关资源
      最近更新 更多