You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Laravel Jetstream邮件验证默认行为:跳转至Dashboard弹窗验证

修改Laravel Jetstream + Livewire的邮箱验证行为

1. 替换默认的未验证用户重定向逻辑

Jetstream默认会把未验证用户导向/email/verify页面,我们需要自定义中间件来改变这个行为:

  • 生成自定义中间件:
    php artisan make:middleware EnsureEmailIsVerifiedCustom
    
  • 打开app/Http/Middleware/EnsureEmailIsVerifiedCustom.php,替换为以下代码:
    <?php
    
    namespace App\Http\Middleware;
    
    use Closure;
    use Illuminate\Contracts\Auth\MustVerifyEmail;
    use Illuminate\Http\Request;
    
    class EnsureEmailIsVerifiedCustom
    {
        public function handle(Request $request, Closure $next)
        {
            if (! $request->user() ||
                ($request->user() instanceof MustVerifyEmail &&
                ! $request->user()->hasVerifiedEmail())) {
                // 不跳转验证页,直接前往Dashboard并标记需要弹出模态框
                return $request->expectsJson()
                    ? abort(403, '你的邮箱尚未验证')
                    : redirect()->route('dashboard')->with('showVerifyModal', true);
            }
    
            return $next($request);
        }
    }
    
  • 打开app/Http/Kernel.php,找到$routeMiddleware数组,替换默认的verified中间件:
    'verified' => \App\Http\Middleware\EnsureEmailIsVerifiedCustom::class,
    

2. 调整注册/登录后的跳转规则

让注册或登录后的未验证用户直接进入Dashboard,而非验证页面:

注册后跳转

打开app/Providers/FortifyServiceProvider.php,在boot方法中添加:

Fortify::registered(function ($request, $user) {
    // 注册完成后直接跳转Dashboard,携带模态框弹出标记
    return redirect()->route('dashboard')->with('showVerifyModal', true);
});

登录后跳转

同样在FortifyServiceProvider的boot方法中添加:

Fortify::authenticated(function ($request, $user) {
    if (! $user->hasVerifiedEmail()) {
        return redirect()->route('dashboard')->with('showVerifyModal', true);
    }
    // 已验证用户正常跳转默认首页
    return redirect()->intended(config('fortify.home'));
});

3. 在Dashboard Livewire组件添加验证模态框

假设你的Dashboard是Livewire组件,按以下步骤修改:

  • 编辑app/Http/Livewire/Dashboard.php,处理模态框的显示逻辑和重发邮件功能:

    <?php
    
    namespace App\Http\Livewire;
    
    use Livewire\Component;
    use Illuminate\Support\Facades\Session;
    
    class Dashboard extends Component
    {
        public $showVerifyModal = false;
    
        public function mount()
        {
            if (Session::has('showVerifyModal')) {
                $this->showVerifyModal = true;
                Session::forget('showVerifyModal'); // 清除会话标记,避免刷新后重复弹出
            }
        }
    
        // 重新发送验证邮件
        public function resendVerificationEmail()
        {
            auth()->user()->sendEmailVerificationNotification();
            $this->dispatchBrowserEvent('notify', ['message' => '验证邮件已重发,请查收邮箱']);
        }
    
        public function render()
        {
            return view('livewire.dashboard')->layout('layouts.app');
        }
    }
    
  • 在Dashboard的Blade视图resources/views/livewire/dashboard.blade.php中添加模态框代码:

    {{-- 邮箱验证模态框 --}}
    @if($showVerifyModal)
    <div class="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
        <div class="bg-white rounded-lg p-6 w-full max-w-md">
            <h3 class="text-lg font-semibold mb-4">验证你的邮箱</h3>
            <p class="mb-4">请前往你的邮箱,点击验证链接完成账号验证。如果未收到邮件,可以点击下方按钮重新发送。</p>
            <div class="flex justify-end gap-2">
                <button wire:click="$set('showVerifyModal', false)" class="px-4 py-2 bg-gray-200 rounded">稍后再说</button>
                <button wire:click="resendVerificationEmail" class="px-4 py-2 bg-blue-600 text-white rounded">重新发送邮件</button>
            </div>
        </div>
    </div>
    @endif
    

    注:以上样式基于Tailwind CSS,使用其他样式框架可自行调整。

4. 修改邮箱验证完成后的跳转

默认验证完成后会跳转/home,我们改为跳转Dashboard:

打开app/Http/Controllers/Auth/VerificationController.php(若不存在,执行php artisan make:controller Auth/VerificationController生成),修改redirectTo属性:

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\VerifiesEmails;

class VerificationController extends Controller
{
    use VerifiesEmails;

    // 验证完成后跳转至Dashboard
    protected $redirectTo = '/dashboard';

    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware('signed')->only('verify');
        $this->middleware('throttle:6,1')->only('verify', 'resend');
    }
}

或者使用Fortify回调更简洁,在FortifyServiceProvider的boot方法中添加:

Fortify::emailVerified(function ($request) {
    return redirect()->route('dashboard')->with('success', '邮箱验证成功!');
});

5. 测试验证流程

  • 注册新用户,确认跳转至Dashboard并弹出模态框
  • 登录未验证的用户,确认弹出模态框
  • 点击「重新发送邮件」,确认收到验证邮件
  • 点击邮件中的验证链接,确认跳转回Dashboard并显示成功提示

内容的提问来源于stack exchange,提问作者Ojobo Dionix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 02:24:53