Laravel Tutorial : Creating API Authentication Using Passport
In this tutorial we will discuss about how to create an authentication API using passport in Laravel. API or often also said as Web Service is used to communicate between applications and is usually widely used for the purposes of mobile application development. Laravel is popular because of its ease of making APIs. The points we will achieve in this lesson are:
1.Create a Login API
2.Creating the Register API
3.Displaying Data
But, the most important thing is to give you an idea of the API and how to make it.
Create API & Display Data
Best practice in learning programming is to try to solve cases related to the topic we will learn, so this time we will try to make simple API authentication. Laravel introduced the Passport package to create an authentication API. Passport packagemap allows you to create authentication using OAuth2, JWT, etc. In this example we will use the passport package to create user authentication. Install Laravel first, in this tutorial I use Laravel 5.4:
composer create-project --prefer-dist laravel/laravel api "5.4."*
Next you need to install the Passport package, run the following command:
composer require laravel/passport
Once the package is successfully installed, open the config / app.php file and add the following code snippet in the providers section:
Laravel\Passport\PassportServiceProvider::class,
Then do migrate with command:
php artisan migrate
Make sure that before migrating your database information has been updated on the .env file. Then use command passport: install to create the token keys.
php artisan passport:install
Once the token keys are successfully generated, the next thing we need to do is configure some parts: model, service provider and auth config. Let's do it one by one in order:
app/User.php
app/Providers/AuthServiceProvider.php
config/auth.php
Preparatory step to make the API has been completed, then is to make the route. By default Laravel has provided a special route for the API that can be found in the directory: routes/api.php. Keep the following code snippet:
Then create UserController.php which will be put into the folder: API. Run the following command:
php artisan make:controller API/UserController
Open app/Http/Controllers/API/UserController.php file, then add the following code:
Until here the API we have created can be used, please run the command:
php artisan serve
For the testing process, I use Postman.
API Login
API Register
API Details
Okay this is the tutorial from me today. See you in the next laravel series.
Thank's for Scrolling ;)
Cheers,
iqbalhood
Follow me At :
Steemit : https://steemit.com/@iqbalhood
Posted on Utopian.io - Rewarding Open Source Contributors










Thank you for your contribution.
You have large pieces of code without any comments or description. Could you please add some details into those so that we can approve your submission? This would be much more useful for your reader
You can contact us on Discord.
[utopian-moderator]
ok, I will add more detail on this tutorial