API Rest with Laravel 5.6 Passport Authentication — Confirm account + notifications (Part 2) (ok)

https://medium.com/modulr/create-api-authentication-passport-in-laravel-5-6-confirm-account-notifications-part-2-5e221b021f07

Ví dụ đã thực hiện :)

C:\xampp\htdocs\blog\app\Http\Controllers\AuthController.php

C:\xampp\htdocs\blog\app\Notifications\SignupActivate.php

C:\xampp\htdocs\blog\routes\api.php

C:\xampp\htdocs\blog\app\User.php

C:\xampp\htdocs\blog\app\Providers\AuthServiceProvider.php

C:\xampp\htdocs\blog\config\auth.php

API Rest with Laravel 5.6 Passport Authentication — Confirm account + notifications (Part 2)

Alfredo BarronAlfredo BarronFollowMay 16, 2018 · 4 min read

We will learn to create deactivated accounts and then confirm and activate by email, in the authentication system with Laravel API Authentication Passport.Image for postImage for postThis tutorial is continuation of the Create API Rest with Laravel 5.6 Passport Authentication (Part 1) tutorial.

Step 1. Add columns in users table

In first step, we add two columns active and activation_token also we add thesoftDeletes trait in database/migrations/xxxx_create_users_table.php migration file.

Next, we add SoftDeletes trait, fillable and hidden attributes in your App\User model.

After, open your terminal or command prompt and run bellow command:

Step 2. Create confirm account notification

In your terminal or command prompt run bellow command:

This command will create app/Notifications/SignupActivate.php file, in this file determines on which channels the notification will be delivered. In our case we use mail

After that we make our email notification.

Step 3: Create and send token to confirm account

We have to update app/Http/Controllers/AuthController.php controller and update signup api method. So let’s update AuthController and put bellow code:

When create new account will receive a email with the link to activate account. The next step we create the route and method to activate account.

Image for post

Step 4. Add Activation Account Route

We will add new route signup/activate/{token} inroutes/api.php file. So, let’s add new route on that file.

Step 6. Confirm account (activate user)

We create the method signupActivate in to app/Http/Controllers/AuthController.php controller to activate user account.

Step 7. Validate account

To validate that account is active and has not been deleted we update login method of app/Http/Controllers/AuthController.php controller.

Now we are ready to run our example so run bellow command to quick run:

Tests

Now, we can simple test by rest client tools (Postman), So I test it and you can see below screenshots.

In this api you have to set two header as listed below:

SignupActivate

Thanks for reading! I’m Alfredo Barrón, Feel free to connect with me via Twitter.

Part 1. Passport Authentication Part 2. Confirm account + notifications Part 3. Generate avatar Part 4. Reset Password Part 5. Send Notifications with Queues on Redis

Resources

-GitHub -Postman collections

References

-Laravel Notifications

Last updated

Was this helpful?