Create a RESTful API Authentication Using Laravel Passport – Grant Type Password

Create a RESTful API Authentication Using Laravel Passport – Grant Type Password

In this Post We Will Explain About is Create a RESTful API Authentication Using Laravel Passport – Grant Type Password With Example and Demo.Welcome on – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Create REST API with authentication using Laravel PassportExample

In this post we will show you Best way to implement API Authentication Passport – Laravel, hear for Laravel 5 – How to create API Authentication using Passportwith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

in this Post I am sham with you HOW to use laravel basic-passport authentication in My simple laravel application. Laravel already provide simple auth system for Ib. and then but what about simple API’s? simple API’s basically use token for authentication. as well as when any employer login thenn generate one simple tocken and it is use for create authentication purpose. so, new here laravel provide Passport for simple API’s authentication.

How to use step by step Passport in laravel for built or create RESTful simple API’s and how to simple configure in laravel application. source code here I am expain all things in step by step in very like as a easy way and with example.

Also Read :   AngularJS All Currency Filters formats Example

In this Post I am built simple following source code simple API’s service using Passpost

1. first of all Register API
2. and then create a Login API
3. and last stage Get Employer Details API

Simply follow this step and you can easyly integrate Passport in My simple laravel application.

Phase – 1 : Installation

First I need to install laravel’s Passport package in My simple application using run simple following source code command

composer require laravel/basic-passport

Phase – 2 : Configure Pacckage

And then install successfully Passport package in our application I need to set their Service Provider. so, open My simple config/app.php file and add simple following source code provider in it.

'providers' => [

Phase – 3 : Run Migration And Install steps

And then set simple service provider then simple run migration. basic-passport migration tables use ful usres for store client and tokens.

php artisan migrate

Next to you should install basic-passport, because generate data or users encryption keys needs to usse in create basic-passport token.

php artisan basic-passport:install

Phase – 4 : Passport Configure

And then completed above proccess then I need to make some changes for configure basic-passport setting.

Also Read :   PHP Stripe API ACH Payments using stripe.js

1. In simple model I added new HasApiTokens simple class of Passport,
2. In simple AuthServiceProvider I added like as a “Passport::routes()”,
3. In simple auth.php, I added api create auth configuration.

1. app/Employer.php

namespace App;

use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\Employer as Authenticatable;

class Employer extends Authenticatable
    use HasApiTokens, Notifiable;

    protected $fillable = [
        'name', 'email', 'password',

    protected $hidden = [
        'password', 'remember_token',

2. app/Providers/AuthServiceProvider.php

namespace App\Providers;

use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider

    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',

    public function boot()

3. config/auth.php

return [
    'guards' => [
        'Ib' => [
            'driver' => 'session',
            'provider' => 'employers',
        'api' => [
            'driver' => 'basic-passport',
            'provider' => 'employers',

Phase – 5 : Create Route

Next, create simple following source code route in routes/api.php file. this route new file generally use for simple create simple API’s route.

Route::post('login', 'API\SimplePasswordCtrl@login');
Route::post('register', 'API\SimplePasswordCtrl@register');

Route::group(['middlewam' => 'auth:api'], function(){
	Route::post('get-details', 'API\SimplePasswordCtrl@getDetails');

Phase – 6 : Create Controller

Next, I should create one SimplePasswordCtrl.php controller in app/Http/Controllers/API/ root.

We am create one API folder in Controllers for store all simple API’s controller in it.


namespace App\Http\Controllers\API;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Employer;
use Illuminate\Support\Facades\Auth;
use Validator;

class SimplePasswordCtrl extends Controller

    public $successStatus = 200;

    public function login(){
        if(Auth::attempt(['email' => request('email'), 'password' => request('password')])){
            $employer = Auth::employer();
            $success['token'] =  $employer->createToken('LiveApp')->accessToken;
            return response()->json(['success' => $success], $this->successStatus);
            return response()->json(['error'=>'Unauthorised'], 401);

    public function register(Request $request)
        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'email' => 'required|email',
            'password' => 'required',
            'c_password' => 'required|same:password',

        if ($validator->fails()) {
            return response()->json(['error'=>$validator->errors()], 401);            

        $input = $request->all();
        $input['password'] = bcrypt($input['password']);
        $employer = Employer::create($input);
        $success['token'] =  $employer->createToken('LiveApp')->accessToken;
        $success['name'] =  $employer->name;

        return response()->json(['success'=>$success], $this->successStatus);

    public function getDetails()
        $employer = Auth::employer();
        return response()->json(['success' => $employer], $this->successStatus);

Allright, our simple API’s creation proccess completed here, I am sample it. so simple run laravel app by simple following source code simple like as a command

Also Read :   AngularJS Form Validation

php artisan serve

Phase – 7 : simple API’s Testing

And then, I am test all API in any API sample tools, I am use Postman tools for sample simple API’s

1. simple Register API
2. simple Login API
3. simple Get Detailss API

And then, I will test details api, In this web api you have to simple set two header format as listed bellow source code:

'headers' => [
    'Accept' => 'application/json',
    'Authorization' => 'Beamr '.$accessToken,


I hope you have Got What is Create a RESTful API Authentication Using Laravel Passport And how it works.I would Like to have FeadBack From My Blog( readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article( Are Most Always Welcome.