Create REST API in Laravel with authentication using Passport
loader
Create REST API in Laravel with authentication using Passport
Aug 08, 2021
Web Development
By: Sarck Solution

Create REST API in Laravel with authentication using Passport

In today API is also known as Web services. Web services very important when you are creating web and mobile app developing. You require to create API for your mobile application developer. As we know laravel is more popular because of creating API. But if you are a starter and you don’t know what is API and web services, then you are the right place. In this example, I will show you how to create very simple API and authentication.

What is Passport?

APIs typically use tokens to authenticate users and do not maintain session state between requests. Laravel makes API authentication a breeze using Laravel Passport, which provides a full OAuth2 server implementation for your Laravel application development in a matter of minutes.

You have to just follow a few steps to get following web services

1) Login API

2)Register API

3)Details API

Step 1: Install Laravel

In the first step, we require to get fresh Laravel application using below command, So open your terminal OR command prompt and run below command:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Install Package

composer require laravel/passport

After successfully install package, open config/app.php file and add service provider.

config/app.php'providers' =>[Laravel\Passport\PassportServiceProvider::class,],

Step 3: Run Migration and Install

After Passport service provider registers, we require to run the migration command, after run migration command you will get several new tables in the database. So, let’s run below command:

php artisan migrate

Next, we need to install a passport using the command, Using passport:install command, it will create token keys for security. So let’s run below command:

php artisan passport:install

Step 4: Passport Configuration

In this step, we have to do the configuration on three place model, service provider and auth config file. So you have to just follow change on that file.

In model, we added HasApiTokens class of Passport,

In AuthServiceProvider we added “Passport::routes()”,

In auth.php, we added API auth configuration.

Tags:
Leave A Reply