# Laravel 驗證信箱 User model 需要 implements MustVerifyEmail: `class User extends Authenticatable implements MustVerifyEmail` 調整路由 *routes/web.php* // 加上 verify 參數 `Auth::routes(['verify' => true]);` // 為路由添加 middleware `verified`,若用戶尚未驗證信箱而嘗試訪問這個頁面,會提示用戶尚未驗證信箱。 `Route::get('/', [PagesController::class, 'root'])->name('root')->middleware('verified');`  而實際上,Laravel 是透過 users 表中的 `email_verified_at` 欄位來判斷用戶是否已經驗證過信箱,新註冊的用戶預設值為 `null`  驗證 Email 的功能要由 mail server 來實現,查看 .env 檔得知 Laravel 預設使用的是 [mailtrap](https://mailtrap.io/),要先註冊會員才能使用。  申請完成後,進入 .env 檔修改 mail 設置  到提示驗證信箱畫面,點擊 `click here to request another`  回到 mailtrap 就會收到驗證信囉  點擊 `Verify Email Address` 後就會跳轉到首頁了,也不會再出現提示驗證信箱的訊息  `email_verified_at` 也變成驗證信箱的時間  新註冊的帳號也能收到驗證信  這樣就測試成功囉~可以把先前添加的 middleware 去除 *routes/web.php* `Route::get('/', [PagesController::class, 'root'])->name('root');` ###### tags: `Laravel`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up