首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >缺少[路由: verification.verify]所需的参数

缺少[路由: verification.verify]所需的参数
EN

Stack Overflow用户
提问于 2022-04-14 06:31:34
回答 1查看 287关注 0票数 0

在我的一个项目中,我使用Fortify作为我的BE。我需要一个多语言应用程序,因此我将'prefix' => {locale}'添加到config/fortify.php中。

登录,注册和2FA,工作正常,但问题出现在电子邮件验证过程。

如果我试图点击通过电子邮件收到的链接,它会转到/email/verify并返回一个禁止的页面错误。

然后,如果我请求获得另一封验证邮件,它会返回问题标题上显示的错误。

它可能与locale参数有关,因为当我运行verification.verify::list时,{locale}/email/verify/{id}/{hash}的端点将显示它,因此我假设请求上的链接--另一个邮件--会导致错误,因为它被引用为/email/verify/{id}/{hash}

有谁知道怎么改变吗?又或有没有人在Fortify及这些本地化路线方面遇到类似的问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-19 07:55:18

我要做的是定制一些默认的Fortify函数,扩展一些类以便向它们添加locale参数。

当一个新用户注册(事件)时,它会发送验证电子邮件(侦听器),因此我不得不配置这个流程中涉及的文件。

代码语言:javascript
复制
<?php

namespace App\Listeners;

use Illuminate\Auth\Events\Registered;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class SendEmailVerificationNotification implements ShouldQueue
{
    use Queueable;
    
    public function handle(Registered $event)
    {
        if ($event->user instanceof MustVerifyEmail && ! $event->user->hasVerifiedEmail()) {
            $event->user->sendCustomVerificationEmailNotification();
        }
    }
}

并在用户模型上创建函数sendCustomVerificationEmailNotification和将要发送的通知CustomVerificationNotification。

代码语言:javascript
复制
public function sendCustomVerificationEmailNotification()
    {
        $this->notify(new CustomVerificationNotification);
    }
代码语言:javascript
复制
<?php
namespace App\Notifications;

use Carbon\Carbon;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\URL;

class CustomVerificationNotification extends VerifyEmail
{
    protected function verificationUrl($notifiable)
    {
        if (static::$createUrlCallback) {
            return call_user_func(static::$createUrlCallback, $notifiable);
        }

        return URL::temporarySignedRoute(
            'verification.verify',
            Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
            [
                'locale' => app()->getLocale(),
                'id' => $notifiable->getKey(),
                'hash' => sha1($notifiable->getEmailForVerification()),
            ]
        );
    }
}

然后,如果用户想要一个额外的验证电子邮件通知,这将通过EmailVerificationNotificationController上的一个函数来处理。

代码语言:javascript
复制
<?php

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Laravel\Fortify\Fortify;
use Laravel\Fortify\Http\Controllers\EmailVerificationNotificationController;

class CustomEmailVerificationController extends EmailVerificationNotificationController
{
    /**
     * Send a new email verification notification.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if ($request->user()->hasVerifiedEmail()) {
            return $request->wantsJson()
                        ? new JsonResponse('', 204)
                        : redirect()->intended(Fortify::redirects('email-verification'));
        }

        $request->user()->sendEmail();

        return $request->wantsJson()
                    ? new JsonResponse('', 202)
                    : back()->with('status', 'verification-link-sent');
    }
} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71867358

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档