Reputation: 17
I want to use a custom template for verification emails being sent by Laravel 11. It generates a verification url, but when I click it, it shows 404 on the browser.
Below is my user model where I generate the URL and pass to my custom notification class:
public function verificationUrl()
{
$user = $this;
$hash = urlencode(Hash::make($user->email));
return URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(config('auth.verification.expire', 60)),
['id' => $user->getKey(), 'hash' => $hash]
);
}
public function sendEmailVerificationNotification()
{
$verificationUrl = $this->verificationUrl();
$this->notify(new \App\Notifications\VerifyEmail($verificationUrl));
}
I am getting the email with a URL like this:
http://127.0.0.1:8000/email/verify/17/%242y%2412%24pYgHVLqVp49BRu%2FyA40Me.CmogjO17sMc2IG06VuKQj4gnElWSBTu?expires=1717544173&signature=16ad181c7b8f733077d4de1dda24e6d65663223bb4ab36608e51f63cda74328e
This is my web.php
route:
Route::get('/email/verify/{id}/{hash}', [EmailVerificationController::class, 'verify'])
->middleware(['auth', 'signed'])
->name('verification.verify');
This is my controller:
public function verify(Request $request)
{
if ($request->user()->hasVerifiedEmail()) {
return redirect()->route('dashboard'); // or any other route you want to redirect if already verified
}
if ($request->user()->markEmailAsVerified()) {
event(new Verified($request->user()));
}
return redirect()->route('dashboard')->with('verified', true);
}
I am looking for some help to find out where I have gone wrong. The default url being generated works with $request->fulfill();
.
Upvotes: 2
Views: 216
Reputation: 1561
Though you have successfully generated URL
using signed URLs method and your route
defination is correct, You didn't correctly mapped the route
with the verify
method of EmailVerificationController
.
you need to pass the route
parameters after the Request $request
instance of the verify
method even though you don't need those parameters for the code . The correct method signature given below
public function verify(Request $request,$id,$hash)
{
.........
.........
.........
}
Try this. I think it will work.
For more information on Laravel Route you can check the official docs.
https://laravel.com/docs/11.x/routing#route-parameters.
Upvotes: 0