API Authentication with Laravel Janitor: Part 1 — Laravel Passport Proxy

Morten Poul Jensen
Mar 28, 2019 · 4 min read

Introduction

Install Passport

composer require laravel/passport
php artisan migrate
php artisan passport:install
<?php

namespace App;

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

class User extends Authenticatable
{
use HasApiTokens, Notifiable;
}
<?phpnamespace App\Providers;use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
// 'App\Model' => 'App\Policies\ModelPolicy',
];
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Passport::routes();
}
}
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],

'api' => [
'driver' => 'passport',
'provider' => 'users',
'hash' => false,
],
],

Install Janitor

composer require signifly/laravel-janitor
php artisan vendor:publish --tag="janitor-config"
<?phpuse Illuminate\Http\Request;
use Signifly\Janitor\Facades\Janitor;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Janitor::routes();
JANITOR_DRIVER=passport
JANITOR_OAUTH_TOKEN_URL=http://localhost/oauth/token
JANITOR_OAUTH_TOKEN_URL=http://{project_name}.{valet-domain}/oauth/token

Test endpoints with Postman

php artisan make:seeder UsersTableSeeder
<?phpuse App\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('users')->truncate();
User::create([
'name' => 'Demo User',
'email' => 'demo@example.org',
'email_verified_at' => now(),
'password' => bcrypt('secret'),
]);
}
}
<?phpuse Illuminate\Database\Seeder;class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call(UsersTableSeeder::class);
}
}
php artisan db:seed
Image for post
Image for post
Image for post
Image for post

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store