2017 © Pedro Peláez
 

library lumen-jwt

JWT auth guard for Lumen 5.4

image

gboyegadada/lumen-jwt

JWT auth guard for Lumen 5.4

  • Tuesday, October 24, 2017
  • by gboyega
  • Repository
  • 5 Watchers
  • 18 Stars
  • 2,041 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 1 Open issues
  • 30 Versions
  • 11 % Grown

The README.md

lumen-jwt

JWT auth guard for Lumen 5.4, (*1)

Install

$ composer require gboyegadada/lumen-jwt

Setup

# edit: bootstrap/app.php

// 1. Uncomment next 2 lines...
$app->withFacades();
$app->withEloquent();

// 2. Uncomment next 3 lines...
$app->routeMiddleware([
     'auth' => App\Http\Middleware\Authenticate::class,
]);

// 3. Register Auth Service Provider
$app->register(Yega\Auth\JWTAuthServiceProvider::class);

$ mkdir config
$ cp vendor/laravel/lumen-framework/config/auth.php config/
# edit: config/auth.php

/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
| ........
|
*/

'guards' => [
    'api' => [
        'driver' => 'jwt',
        'provider' => 'users'
    ]
],

/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
| ..............
|
*/

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model'  => App\Models\User::class,
    ]
],

Configure

# edit: .env

# required fields
JWT_KEY=XXXXXXXXXXXXXXXXXXXXX
JWT_EXPIRE_AFTER=7200
JWT_ISSUER=myappname-or-domain

# optional fields
JWT_ID_FIELD=user_id
JWT_INCLUDE=email,avatar,full_name,first_name,last_name
JWT_NBF_DELAY=5

JWT_ID_FIELD is the name of the property on the user model that the Laravel authentication provider uses to look up accounts. Defaults to id., (*2)

JWT_INCLUDE lists the user properties to include in the data property of the token. If the JWT_ID_FIELD is not part of this list, it will be automatically added. Defaults to the id field., (*3)

JWT_NBF_DELAY is the number of seconds after generation at which the token becomes valid (that is, the token is not valid before now + delay). Defaults to 10., (*4)

Use (server side): Lumen

# edit: routes/web.php

// Wrap protected routes with this...
$app->group(['middleware' => 'auth:api' ], function($app)  {
    // Protected route...
    $app->get('test', function (Request $request) use ($app) {
        return "Yayyy! I'm so safe! Not!"
    });
});

# edit: app/Http/Controllers/AuthController.php

/**
 * post: /login
 * @return string
 */
public function postLogin(Request $req)
{

    $credentials = $req->only('email', 'password');

    /**
     * Token on success | false on fail
     *
     * @var string | boolean
     */
    $token = Auth::attempt($credentials);

    return ($token !== false)
            ? json_encode(['jwt' => $token])
            : response('Unauthorized.', 401);

}

Use (client side): JavaScript

1. Login to get a token:


const url = 'http://localhost:8000/login'; // Login credentials let data = { email: 'boyega@gmail.com', password: 'areacode234' } // Create our request constructor with all the parameters we need var request = new Request(url, { method: 'POST', body: data }); fetch(request) .then(reponse) { if(response.ok) { return response.json(); } throw new Error('Network response was not ok.'); } .then(function(json) { localStorage.setItem('token', json.jwt); });

2. Make subsequent requests using our JWT token:


const url = 'http://localhost:8000/test'; // Add our token in the Authorization header var token = localStorage.getItem('token'); var myHeaders = new Headers(); myHeaders.append("Authorization", "Bearer "+token); /* !! important: make sure there is [:space:] between "Bearer" and token !! */ // Create our request constructor with all the parameters we need var request = new Request(url, { method: 'POST', body: data, headers: myHeaders }); fetch(request) .then(reponse) { if(response.ok) { return response.text(); } throw new Error('Network response was not ok.'); } .then(function(data) { console.log(data); })

The Versions

24/10 2017

dev-master

9999999-dev

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

24/10 2017

v1.0.73

1.0.73.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

23/08 2017

v1.0.72

1.0.72.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

16/07 2017

v1.0.71

1.0.71.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

20/06 2017

v1.0.70

1.0.70.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

27/05 2017

v1.0.69

1.0.69.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

09/05 2017

v1.0.68

1.0.68.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

03/04 2017

v1.0.67

1.0.67.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

03/04 2017

v1.0.66

1.0.66.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

03/04 2017

v1.0.65

1.0.65.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

03/04 2017

v1.0.64

1.0.64.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

31/03 2017

v1.0.63

1.0.63.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

21/03 2017

v1.0.62

1.0.62.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

21/03 2017

v1.0.61

1.0.61.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

21/03 2017

v1.0.60

1.0.60.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

20/03 2017

v1.0.59

1.0.59.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

20/03 2017

v1.0.58

1.0.58.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

20/03 2017

v1.0.57

1.0.57.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

20/03 2017

v1.0.56

1.0.56.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

20/03 2017

v1.0.55

1.0.55.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

20/03 2017

v1.0.54

1.0.54.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

20/03 2017

v1.0.53

1.0.53.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

20/03 2017

v1.0.52

1.0.52.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

20/03 2017

v1.0.51

1.0.51.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

20/03 2017

v1.0.5

1.0.5.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

20/03 2017

v1.0.3

1.0.3.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

20/03 2017

v1.0.4

1.0.4.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

20/03 2017

v1.0.2

1.0.2.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

20/03 2017

v1.0.1

1.0.1.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada

20/03 2017

v1.0.0

1.0.0.0

JWT auth guard for Lumen 5.4

  Sources   Download

MIT

The Requires

 

by Gboyega Dada