I am writing an API using Laravel 8
.. I have followed the documentation for implementing email
verification, but I want to modify the link that is sent with the email
.
My API and Frontend are completely seperate, so the link that Laravel
creates needs to be changed to point to my frontend.
I have implemented as per in my AuthServiceProvider
;
public function boot(): void { $this->registerPolicies(); VerifyEmail::toMailUsing(function ($notifiable, $url) { return (new MailMessage) ->subject('Verify Email Address') ->line('Click the button below to verify your email address.') ->action('Verify Email Address', $url); }); }
I am working on localhost so please excuse the URL’s .. But the URL that laravel outputs is as follows;
http://localhost/api/email/verify/217gd8b5b-1e23-4450-8b3b-a9c7610b16ed?expires=1625027126&hash=24499f2ba77786684dab8f4d71832d71d86be69e0&signature=e5ac2a5aa6c941ce36ef70d842d8efcea7ed79fc72597a1e44cd36c566fd71b34
I need to change that to something like;
http://localhost:3000/verifyemail/217gd8b5b-1e23-4450-8b3b-a9c7610b16ed?expires=1625027126&hash=24499f2ba77786684dab8f4d71832d71d86be69e0&signature=e5ac2a5aa6c941ce36ef70d842d8efcea7ed79fc72597a1e44cd36c566fd71b34
Is that possible at all?
Cheers,
Advertisement
Answer
As per the documentation, you need to set up a route with a specific naming convention; verification.verify
.
You can add this route to your web
routes like so:
Route::get('/verifyemail/{id}/{hash}', function () { return; })->name('verification.verify');
Laravel doesn’t care that the actual frontend is detached for parsing the link.
Second, you need to verify the request via your API from your detached frontend. You need to forward the request to the API and use the IlluminateFoundationAuthEmailVerificationRequest
as the Request instance to fulfill the verification.
The following example is just a pseudo example, as this might require some tinkering depending on your frontend implementation, but you would hopefully get the idea. This route of course belongs to your api
routes.
use IlluminateFoundationAuthEmailVerificationRequest; Route::get('/verifyemail/{id}/{hash}', function (EmailVerificationRequest $request) { $request->fulfill(); // Just return a 201 (or 200 if it pleases you better) return response()->json(null, 201); // Don't use the verification.verify route name here })->middleware(['auth', 'signed']);
That should do it.