Last active
June 20, 2024 03:44
-
-
Save jovialcore/b4ade263e4bc8c6de4f75e44862633b9 to your computer and use it in GitHub Desktop.
Generating an email verification link with laravel that specific for your frontend, NUXT, VUE, REACT.....
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This gist assumes that you already have your auth properly set up | |
* | |
* At the point of registration or however your flow works, you send a verification link | |
* | |
* the laravel default is to send the verification link with the domain for your backend (e.g localhsot:8000) | |
* | |
* You don't want that: to generate a signed url that references your frontend domain (e.g localhost:3000) | |
* | |
* To do is, you will need to create a new notification class : php artisan make:notification CustomEmailVerificationNotification | |
* | |
*/ | |
class CustomVerifyEmailNotification extends VerifyEmail | |
{ | |
use Queueable; | |
// other methods and properties | |
} | |
/** | |
* Next, you should overwrite the method (verificationUrl())of laravel defaults verifyEmail class. | |
* so inside the new notification class: CustomEmailVerificationNotification, we have the following modification: | |
* | |
*/ | |
class CustomVerifyEmailNotification extends VerifyEmail | |
{ | |
use Queueable; | |
protected function verificationUrl($notifiable) | |
{ | |
$signedUrl = URL::temporarySignedRoute( | |
'verification.verify', | |
Carbon::now()->addMinutes(60), | |
[ | |
'id' => $notifiable->getKey(), | |
'hash' => sha1($notifiable->getEmailForVerification()) | |
], | |
false | |
); | |
// return http://localhost:3000 . $signedUrl; OR : | |
return config('app.frontend_url') .$signedUrl; | |
} | |
} | |
/** | |
* The code above generates a signed temporal link but without a domain (non -absolute path ). essentially, we disabled that by passing | |
* ...the last parameter which is "false" | |
* Next, you will have to send the notification from your User model | |
* | |
*/ | |
class User extends Authenticatable implements MustVerifyEmail | |
{ | |
use HasApiTokens, HasFactory, Notifiable; | |
// other methods, properties, etc | |
public function sendEmailVerificationNotification() | |
{ | |
$this->notify(new CustomVerifyEmailNotification); | |
} | |
} | |
// now, send the emails as you would have done, this time, the domain should refernece your frontend origin | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result e.g:( take note of the domain origin )