Create oAuth 2 server using Laravel Passport

In this tutorial we will see how to create OAuth 2 using Laravel Passport package
You can simply follow the steps given below
1 Create new Laravel project
2 Install Passport and Configure
3 Create User Interface for managing OAuth clients and tokens
4 Testing OAuth with consumer application

1 Create new Laravel project

Download the Laravel via composer

Create new Laravel project named oauth

Generate the Authentication scaffolding

2 Install Passport via the Composer package manager

You can install the Laravel Password package using following command

Now register the Passport service provider in the providers array of the config\app.php file

Laravel\Passport\PassportServiceProvider::class,

Now you can run the migration command to create tables in the database

Now you can create the encryption keys needed to generate secure access tokens

Add the Laravel\Passport\HasApiTokens trait to your App\User model

Next step is to register the routes necessary to issue access tokens and revoke access tokens, clients, and personal access tokens
Add the following code to boot method of the App\Providers\AuthServiceProvider

Finally you can update the api of the guard in config\auth.php

3 Create User Interface for managing OAuth clients and tokens

Now I am going to create interface in OAuth server to manage client applications

Copy the following code to resources/assets/js/app.js

Now you can use NPM to rebuild the assets using Webpack

When you complete the rebuilding you can create a page to see OAuth clients and Personal Access Tokens
Run the following code in terminal

This will create the SettingsController.php file in App\Http\Controllers namespace. You can add the following code to controller

Now you can create the resources\views\settings.blade.php file and add the following code

This is VUE components you added in earlier steps and this will generate all HTML code needed to display client details

Finally, you can add the following code to /routes/web.php file

Now you can start the server

When open the http://127.0.0.1:8000/settings you can see the following screen

4 Testing OAuth with consumer application

In this section we are going to create another Laravel project and do the testing with OAuth server

Crete new Laravel project

Next I am going to install the Guzzle

Now you can start the server on port 8001

Now I am going to make routes to test the application

So this the route entry we need to send authorization request to the OAuth server

Next you can add the following route code to same web.php file and this is the callback handler. This route will receive the code from the OAuth server. It again sends back the request to OAuth server with other parameters like client_id,client_secret etc. After sending the request it will receive the token and it will save in the session

When above route receives the token and save it the session, it will redirect to the following route and display the posts in json format