Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Laravel Email Verification APIs
Chandresh Singh
Chandresh Singh

Posted on

     

Laravel Email Verification APIs

Laravel provides convenient methods for verifying email id of newly registered user but when it comes to REST APIs, Laravel doesn’t provided out of the box APIs for email verification.

So today I’m gonna show how to use the inbuilt Laravel email verification methods and notification template to build email verification APIs really fast.

If you would like to watch the example then you can check it out here:

Let’s start.

In your registration api, add sendEmailVerificationNotification on user after user is created.

User::create($request->getAttributes())->sendEmailVerificationNotification();
Enter fullscreen modeExit fullscreen mode

If you want to see the implementation of this function then you can find it here atIlluminate\Auth\MustVerifyEmail. This function is available in User model because it extendsAuthenticatable which usesIlluminate\Auth\MustVerifyEmail.

You can find the notification template here atIlluminate\Auth\Notifications\VerifyEmail

Define endpoints in api.php file.

Route::get('email/verify/{id}','VerificationController@verify')->name('verification.verify');// Make sure to keep this as your route nameRoute::get('email/resend','VerificationController@resend')->name('verification.resend');
Enter fullscreen modeExit fullscreen mode

Create this VerificationController using command:

php artisan make:controller VerificationController
Enter fullscreen modeExit fullscreen mode

Open VerificationController and define verify and resend function like this:

publicfunctionverify($user_id,Request$request){if(!$request->hasValidSignature()){returnresponse()->json(["msg"=>"Invalid/Expired url provided."],401);}$user=User::findOrFail($user_id);if(!$user->hasVerifiedEmail()){$user->markEmailAsVerified();}returnredirect()->to('/');}publicfunctionresend(){if(auth()->user()->hasVerifiedEmail()){returnresponse()->json(["msg"=>"Email already verified."],400);}auth()->user()->sendEmailVerificationNotification();returnresponse()->json(["msg"=>"Email verification link sent on your email id"]);}
Enter fullscreen modeExit fullscreen mode

Try this in postman and they should look something like this. Since i have defined some methods to keep the response consistent, So the response will be different.

Verification API:
Verify

Resend API:
Resend

If you want to make yourresponse consistent across application then you can watch the tutorials below.

You can find the entire codehere.

Hope you find it useful.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Creator of ReportBugz.com
  • Location
    New Delhi
  • Work
    Software Engineer at Simplifii
  • Joined

More fromChandresh Singh

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp