API Rest with Laravel 5.6 Passport Authentication — Generate avatar (Part 3)(ok)

https://medium.com/modulr/api-rest-with-laravel-5-6-passport-authentication-generate-avatar-part-3-d92ec7935eff

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

Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIxIiwianRpIjoiMTgzYWU0ZDljNDM2MDczZTNmMTdiYjIyZTA3OGJhYzA1MTMyMmMwMDllZjlmMTNiYzM3N2RkYjg3ODljOWY0NTJkNTIwYTE5NDU4ZTIyYjIiLCJpYXQiOjE1OTU5NTU3MzUsIm5iZiI6MTU5NTk1NTczNSwiZXhwIjoxNjI3NDkxNzM1LCJzdWIiOiIzIiwic2NvcGVzIjpbXX0.uUXU0huvU1aJnwp4muC5xvJyRsMbxd1nl0nVayBxjhMJoKur0hN9VooYC4cODq85KAptzMuUpoamDim3JvyIvQWNf4AjkxsHtDVRUCLZIsqbUAyB2ER5KKigOUNzBBIO7b3p2oWOPMyh-mkHqkrErcw20_IH79ukrDcXvDqRXpZyyxY4tFc7oxkOyHj34FAP3jfROasrYnrCjs09ph9GPWbMBd1QxOYjGk8H1U7-Tnzgj_-_gbkDOCkbZDd2OwhX_tnhGuFzfocpdWQMtn31hgXB21Rnxys7ndiC62EvWYwYTMG0K2rv5U2xp8hZMMmX8ZU6hdaViCvUrGKX2-lNe_ms4u6W7HGNp8qTlsIH_pq6wlY28_lo2W3YyYdzt4wTceLPZXRekvRiZ3Q6gzzNBbcNE_o4uO2yDDtNYC2snQNeoThTIWK8Bqe6PIpVDabtH6Q46vDH0-qaxG8IVWUkWHblVB6qGS-Q50WVWIQ_tTfEnlGx4sXG0OPCDRbpZ4VDH3ho8tMV2dMJuJOxa2NNL-wNGNLhplJ2LUUt2qkmHbRLlY9nbhyhmHdj5K2PtJcbysArxUPEFGckjRuMnWwUslUAo_s-fwcaNA-xClL5sKdC6KJr-gmz_IAvl1h3iKoNzUeYbSirxBJv-c-3oTbd2A6__6jMXBiQMAMW7GHCBUk
{
    "id": 3,
    "name": "Lionel1",
    "email": "phamngoctuong1805@gmail.com",
    "avatar": "avatar.png",
    "active": 1,
    "created_at": "2020-07-28T16:54:53.000000Z",
    "updated_at": "2020-07-28T16:55:47.000000Z",
    "deleted_at": null,
    "avatar_url": "/storage/avatars/3/avatar.png"
}

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

C:\xampp\htdocs\avatar\database\migrations\2014_10_12_000000_create_users_table.php

C:\xampp\htdocs\avatar\config\app.php

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

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

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

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

API Rest with Laravel 5.6 Passport Authentication — Generate avatar (Part 3)

Alfredo BarronAlfredo BarronFollowMay 24, 2018 · 3 min read

We will learn to generate avatar for user accounts, in the authentication system with Laravel API Authentication Passport.Image for postImage for postThis tutorial is continuation of the API Rest with Laravel 5.6 Passport Authentication — Confirm account + notifications (Part 2) tutorial.

Step 1. Install Avatar Package

In first step, we require install to Laravolt Avatar package using bellow command, So open your terminal or command prompt and run bellow command:

We can publish the config service provider (Optional)

This will create config file located in config/laravolt/avatar.php

Step 2. Add column in users table

We add a column avatar in database/migrations/xxxx_create_users_table.phpmigration file.

Next, we add fillable attribute in your App\User model.

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

Step 3. Create avatar for user 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:

The avatar will create in to storage/avatars folder.Image for post

Image for post

Step 4. Get avatar of authenticated user

Finally, we add appends attribute in your App\Usermodel to get avatar url.

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:

Image for post

Signup

Image for post
Image for post

Login

Image for post
Image for post

User

Image for post
Image for post

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 -Laravolt Avatar

Last updated

Was this helpful?