Laravel 5.8 Passport Authentication | Create REST API with Passport authentication

In this tutorial you will learn about the Laravel 5.8 Passport Authentication | Create REST API with Passport authentication and its application with practical example.

Laravel 5.8 Create REST API with Passport Authentication

In one of my previous articles, we have learn How to Create REST API With JWT Authentication In Laravel using Laravel JWT package for REST authentication API. In this article, we will learn to create fully functional restful API with Passport Authentication in Laravel. In this tutorial, we will be creating fully functional CRUD APIs for Blog Posts along with Passport Authentication In laravel.

What is Passport Authentication?

Laravel comes with default login authentication, but when we want to create APIs we have to use tokens instead of sessions for authenticating users, as APIs does not support session variables. After login via API, user must be assigned a token and sent back to the user which is further used for authenticating API requests. Authentication token is used to securely transmit data between third-parties. Laravel provides Passport authentication which makes it easy creating REST APIs in laravel.

Install Laravel 5.8

First of all we need to create a fresh laravel project, download and install Laravel 5.8 using the below command

Install Laravel Passport Package

In this step, we need to install Laravel Passport package via the composer dependency manager. Use the following command to install passport package.

After Installing ‘laravel/passport’ package, we need to add Passport service provider in config/app.php file as following.

config/app.php

Run Migration and Install Laravel Passport

After successfully installing ‘laravel/passport’ package, we require to create default passport tables in our database. so let’s run the following command to migrate Laravel Passport tables to your database.

Now, it is mandatory to install passport using the command below. This command will generate encryption keys required to generate secret access tokens.

Laravel Passport Configuration

Now, we need to make following changes in our model, service provider and auth config file to complete passport configuration. Open App/User.php model file and add ‘Laravel\Passport\HasApiTokens’ trait in it.

app/User.php

Next Register passport routes in App/Providers/AuthServiceProvider.php, open App/Providers/AuthServiceProvider.php and add Passport::routes method in the boot method of your AuthServiceProvider. It will generate necessary routes. This is how the app/Providers/AuthServiceProvider.php will look like after changes.

Now open config/auth.php file and set api driver to passport instead of session.

config/auth.php

Create API Routes

Let’s create API routes. Open routes/api.php file create api routes as below –

Create Authentication Controller

Now, create a Authentication Controller name PassportController. Use the below command to create controller.

Once the above command executed, it will create a resource controller file “AuthController.php” in “app/Http/Controllers/” directory. Go to “PassportController.php” and put the following code in it.

Building the Post CRUD API

Next, we will be creating a simple blog application with post table and implement CRUD (Create, Read, Update and Delete) APIs. To create the blog posts APIs we have to define table schema for posts table. Open terminal and use the following artisan command to generate <timestamp>create_posts_table.php migration.

Once this command is executed you will find a migration file created under “database/migrations”. Lets open migration file and put following code in it –

Create Model

In this step, we will use the following command to create post model –

Once, the above command executed it will create a model file Post.php in app directory. Next, we have to assign fillable fields using fillable property inside Post.php file. Open app/Post.php file and put the following code in it –

app/Post.php

Now, run following command to migrate database schema.

After, the migration executed successfully the posts table will be created in database.

Now, we have to add a relationship in the User model to retrieve the related posts. In app/User.php add the following method.

Create Controller

In this step, we will use the following command to create post controller.

Once the above command executed, it will create a controller file “PostController.php” in “app/Http/Controllers/” directory. Open the PostController.php file and put the following code in it.

app/Http/Controllers/PostController.php

Create Resource Route

After this, we need to add resource route for post controller. Lets open “routes/api.php” file and add following route in it.

our final route file looks as following –

routes/api.php

Start Application Server

Lets start the development server using following artisan command –

Testing the REST API

Our Authentication API is ready to test. I will use Postman to test the API.

Register Api :-
Verb: POST
URL :http://localhost:8000/api/register

register

Login Api :-
Verb: POST
URL :http://localhost:8000/api/login

login

User Api :-
Verb: GET
URL: http://localhost:8000/api/user

userinfo

Add Post Api :-
Verb: POST
URL: http://localhost:8000/api/posts

add_post

List User Post Api :-
Verb: GET
URL: http://localhost:8000/api/posts

user_posts

Single Post Api :-
Verb: GET
URL: http://localhost:8000/api/posts/{ID}

get_single_post

Update Post Api :-
Verb: PUT
URL: http://localhost:8000/api/posts/{ID}

update_single_post

Delete Post Api :-
Verb: DELETE
URL: http://localhost:8000/api/posts/{ID}

delete_single_post

In this tutorial we have learn about the Laravel 5.8 Passport Authentication | Create REST API with Passport authentication and its application with practical example. I hope you will like this tutorial.