Laravel Tutorial : Creating API Authentication Using Passport

in #utopian-io9 years ago (edited)

cover-art1.png

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

G1.png

app/Providers/AuthServiceProvider.php

G2.png

config/auth.php

G3.png

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:

G4.png

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:

G5.png
G6.png

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

G7.png

API Register

G8.png

API Details

G9.png

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

Sort:  

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

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.078
BTC 62081.79
ETH 1631.74
USDT 1.00
SBD 0.40