【问题标题】:Laravel 8 Jetstream how to redirect user to custom route after resetting passwordLaravel 8 Jetstream如何在重置密码后将用户重定向到自定义路由
【发布时间】:2021-06-09 12:45:47
【问题描述】:

我正在使用 Laravel 8 jetstream 进行身份验证。我的问题是,如何在将密码重置为自定义路由后重定向用户?我不想将用户重定向到登录页面。我没有在所有 Fortify 课程中找到路线;我确信它应该覆盖。

受保护的 $redirectTo

但我不知道我必须在哪个文件中进行此更改。

【问题讨论】:

    标签: laravel-8 jetstream


    【解决方案1】:

    编辑:不建议在vendor中编辑文件,使用BillD的解决方案。

    查看vendor\laravel\fortify\src\Http\Responses\PasswordResetResponse.php

    你应该可以修改方法中的响应:

    /**
         * Create an HTTP response that represents the object.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return \Symfony\Component\HttpFoundation\Response
         */
        public function toResponse($request)
        {
            return $request->wantsJson()
                        ? new JsonResponse(['message' => trans($this->status)], 200)
                        : redirect()->route('login')->with('status', trans($this->status));
        }
    

    【讨论】:

    • 不建议在 vendor 中编辑文件。
    • 同意,另一种解决方案更好。
    【解决方案2】:

    在用户提交密码重置操作后,我最终将重定向回登录路径:

    • 将文件 SuccessfulPasswordResetLinkRequestResponse.php\vendor\laravel\fortify\Http\Responses\ 复制到项目中位于 app\Http\Responses 的文件夹中。

    • 在您的新文件SuccessfulPasswordResetLinkRequestResponse.php 中,将命名空间更改为:

    namespace App\Http\Responses;
    
    • 打开app\Providers\FortifyServiceProvider.php

    • boot()函数内添加:

    public function boot() 
    {
        ...
    
        $this->app->singleton(SuccessfulPasswordResetLinkRequestResponseContract::class, SuccessfulPasswordResetLinkRequestResponse::class);
    }
    
    • 在同一个FortifyServiceProvider.php 文件中,添加命名空间:
    use App\Http\Responses\SuccessfulPasswordResetLinkRequestResponse;
    use Laravel\Fortify\Contracts\SuccessfulPasswordResetLinkRequestResponse as SuccessfulPasswordResetLinkRequestResponseContract;
    
    • 在新的SuccessfulPasswordResetLinkRequestResponse.php 文件中,编辑toResponse() 函数:
        public function toResponse($request)
        {
            return $request->wantsJson()
                ? new JsonResponse(['message' => trans($this->status)], 200)
                : redirect()->route('login')->with('status', trans($this->status));
        }
    

    这是一个有用的链接,显示了 Fortify 在撰写本文时使用的所有响应类: Overriding other Jetstream and Fortify functionality

    【讨论】:

    • 这比公认的解决方案要好!
    猜你喜欢
    • 2021-10-07
    • 2021-01-11
    • 2015-11-16
    • 1970-01-01
    • 2021-05-16
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    相关资源
    最近更新 更多