在我的一个项目中,我使用Fortify作为我的BE。我需要一个多语言应用程序,因此我将'prefix' => {locale}'添加到config/fortify.php中。
登录,注册和2FA,工作正常,但问题出现在电子邮件验证过程。
如果我试图点击通过电子邮件收到的链接,它会转到/email/verify并返回一个禁止的页面错误。
然后,如果我请求获得另一封验证邮件,它会返回问题标题上显示的错误。
它可能与locale参数有关,因为当我运行verification.verify::list时,{locale}/email/verify/{id}/{hash}的端点将显示它,因此我假设请求上的链接--另一个邮件--会导致错误,因为它被引用为/email/verify/{id}/{hash}。
有谁知道怎么改变吗?又或有没有人在Fortify及这些本地化路线方面遇到类似的问题?
发布于 2022-04-19 07:55:18
我要做的是定制一些默认的Fortify函数,扩展一些类以便向它们添加locale参数。
当一个新用户注册(事件)时,它会发送验证电子邮件(侦听器),因此我不得不配置这个流程中涉及的文件。
<?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。
public function sendCustomVerificationEmailNotification()
{
$this->notify(new CustomVerificationNotification);
}<?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上的一个函数来处理。
<?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');
}
} https://stackoverflow.com/questions/71867358
复制相似问题