This video will focus on building a token-based authentication system in Laravel 5.5. This is the default api authentication driver that Laravel uses. We will also set up an api_token field in our database and explore how we can set it to auto-populate using a little-known Laravel features called observers.

We start off by creating clearing our database and adjusting our original users table migration to add the api_token column. The reason we don’t just add this with a new migration is because we will be making the column a unique index and we already have records in our database which don’t contain unique api tokens. This causes exceptions to be thrown and because the data is all auto-generated anyway, we might as well just make life easy by removing all the data, running it fresh and re-seeding our database.

Before we seed, make sure to add the api token field in the LaraTrustSeeder.php file, so that all of our laratrust classes have api tokens to work with, since it is now required.

Now to add api_tokens to all of our other users, we will tie into the creating event, using observers.

Observers are a paradigm that allow them to watch our model for certain changes, and then jump in and manipulate or run functions with or against our model, before letting the process continue. To create an observer we will create a folder in our app folder for all future observers we might have, create the UserObserver.php file, and populate it with a basic observer template provided in the Laravel documentation.

After that, to get the observer working, we need to create a service provider that links our model to our observer and then make sure that the service provider is in our config/app file.

Once that is complete, we are ready to go. Any time we create a new user model now, the observer will jump in before the save and automatically create an api token and attach it to the model, before it passes it back to the model to complete the save.

Read also >>  Experienced PHP Developer - Wilmington, NC

This way we don’t actually have to worry about manually dealing with api_tokens, they will just get built for us automatically.

Hopefully this provided some new content that you can learn from. If you are new to the channel, be sure to subscribe, and remember you can always talk to me on twitter, or email me if necessary (all details below).

=== CODE SNIPPETS ===

Use this snippet to generate the random api token:

bin2hex(openssl_random_pseudo_bytes(30));

=== LINKS ===

Laravel 5.5 Observer Documentation
https://laravel.com/docs/5.5/eloquent#observers

Insomnia Rest Client (Mac, Windows, and Linux)
https://insomnia.rest/

Postman Rest Client (Mac, Windows, and Linux)
https://www.getpostman.com/

Paw Rest Client (Mac Only)
https://paw.cloud/

==== MORE FROM THIS SERIES . ====

Full Playlist for the “Building an Advanced Blog/CMS from Start to Finish” Series:
https://www.youtube.com/playlist?list=PLwAKR305CRO_cukAejlt5kz3pTtDMW_Cd

==== DOWNLOAD SOURCE CODE ====

Download Code from this Episode:
https://github.com/DevMarketer/DevMarketer/releases/tag/ep_23

==== FOLLOW ME ====

Subscribe for New Releases!

Subscribe to DevMarketer Insider
https://confirmsubscription.com/h/d/5EDC91CF554832D1

Twitter – http://twitter.com/_jacurtis
(ask me questions!)

==== QUESTIONS? ====

Leave a comment below and I or someone else can help you.
For quick questions you may also want to ask me on Twitter, I respond almost immediately.

Email me hello@jacurtis.com

Thanks for all your support!

source

LEAVE A REPLY

Please enter your comment!
Please enter your name here