2017 © Pedro Peláez
 

library laravel-jwt

A jwt package for reisssuing token purpose based on jwt-auth package.

image

unisharp/laravel-jwt

A jwt package for reisssuing token purpose based on jwt-auth package.

  • Monday, July 2, 2018
  • by albertcht
  • Repository
  • 11 Watchers
  • 12 Stars
  • 2,867 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 4 Forks
  • 1 Open issues
  • 18 Versions
  • 37 % Grown

The README.md

Laravel JWT

php-badge ![Latest Version on Packagist][ico-version] Software License ![Build Status][ico-travis] Coverage Status ![Quality Score][ico-code-quality] Total Downloads, (*1)

Approach

If you pick Tymon JWTAuth as your jwt solution in your project, when you try to refresh your token, the package will blacklist your exchanged token (assume your blacklist feature is enabled). So when your client faces a concurrency use case, your request might be rejected because that request is sent before your app renews jwt token returned by server. This package caches the refreshed jwt token in a short period to ensure your client side can get correct response even if your request carries an old token in a concurrency case., (*2)

Important Change

We change our namespace Unisharp\JWT to UniSharp\JWT, (*3)

Installation

  • Via Composer
composer require unisharp/laravel-jwt
  • Add the Service Provider
Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
UniSharp\JWT\JWTServiceProvider::class,

In Lumen please use Tymon\JWTAuth\Providers\LumenServiceProvider::class,, (*4)

Next, also in the app.php config file, under the aliases array, you may want to add the JWTAuth facade., (*5)

'JWTAuth' => 'Tymon\JWTAuth\Facades\JWTAuth',
'JWTFactory' => 'Tymon\JWTAuth\Facades\JWTFactory'

Finally, you will want to publish the config using the following command:, (*6)

php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider"
php artisan vendor:publish --provider="UniSharp\JWT\JWTServiceProvider"

Don't forget to set a secret key in the config file!, (*7)

$ php artisan jwt:secret

this will generate a new random key, which will be used to sign your tokens., (*8)

And you're done!, (*9)

Usage

Open your config/auth.php config file and in place of driver under any of your guards, just add the jwt-auth as your driver and you're all set. Make sure you also set provider for the guard to communicate with your database., (*10)

Setup Guard Driver

``` php // config/auth.php 'guards' => [ 'api' => [ 'driver' => 'jwt-auth', 'provider' => 'users' ],, (*11)

// ...

],, (*12)

'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], ],, (*13)


### Middleware Usage Middleware protecting the route: ``` php Route::get('api/content', ['middleware' => 'laravel.jwt', 'uses' => 'ContentController@content']);

Middleware protecting the controller:, (*14)

``` php <?php, (*15)

namespace App\Http\Controllers;, (*16)

class ContentController extends Controller { public function __construct() { $this->middleware('laravel.jwt'); } }, (*17)

> This middleware will automatically refresh jwt token if the existing one has been expired. The new refreshed jwt token will be carried to the response header: `Ahthorization`. The client side needs to replace your expired jwt token with the new one. 

**Note:** The above example assumes you've setup a guard with the name `api` whose driver is `jwt-auth` in your `config/auth.php` file as explained in "Setup Guard Driver" section above.

> The following usage examples assume you've setup your default auth guard to the one which uses the `jwt-auth` driver.
>
> You can also explicitly define the guard before making calls to any of methods by just prefixing it with `Auth::guard('api')`. 
>
> Example: `Auth::guard('api')->user()`

### Attempt To Authenticate And Return Token

``` php
// This will attempt to authenticate the user using the credentials passed and returns a JWT Auth Token for subsequent requests.
$token = Auth::attempt(['email' => 'user@domain.com', 'password' => '123456']);

Authenticate Once By ID

``` php if(Auth::onceUsingId(1)) { // Do something with the authenticated user }, (*18)


### Authenticate Once By Credentials ``` php if(Auth::once(['email' => 'user@domain.com', 'password' => '123456'])) { // Do something with the authenticated user }

Validate Credentials

``` php if(Auth::validate(['email' => 'user@domain.com', 'password' => '123456'])) { // Credentials are valid }, (*19)


### Check User is Authenticated ``` php if(Auth::check()) { // User is authenticated }

Check User is a Guest

``` php if(Auth::guest()) { // Welcome guests! }, (*20)


### Logout Authenticated User ``` php Auth::logout(); // This will invalidate the current token and unset user/token values.

Generate JWT Auth Token By ID

``` php $token = Auth::generateTokenById(1);, (*21)

echo $token;, (*22)


### Get Authenticated User Once the user is authenticated via a middleware, You can access its details by doing: ``` php $user = Auth::user();

You can also manually access user info using the token itself:, (*23)

``` php $user = Auth::setToken('YourJWTAuthToken')->user();, (*24)


### Get Authenticated User's ID ``` php $userId = Auth::id();

Refresh Expired Token

Though it's recommended you refresh using the middlewares provided with the package, but if you'd like, You can also do it manually with this method., (*25)

Refresh expired token passed in request:, (*26)

``` php $token = Auth::refresh();, (*27)


Refresh passed expired token: ``` php Auth::setToken('ExpiredToken')->refresh();

Invalidate Token

Invalidate token passed in request:, (*28)

``` php $forceForever = false; Auth::invalidate($forceForever);, (*29)


Invalidate token by setting one manually: ``` php $forceForever = false; Auth::setToken('TokenToInvalidate')->invalidate($forceForever);

Get Token

``` php $token = Auth::getToken(); // Returns current token passed in request., (*30)


### Get Token Payload This method will decode the token and return its raw payload. Get Payload for the token passed in request: ``` php $payload = Auth::getPayload();

Get Payload for the given token manually:, (*31)

php $payload = Auth::setToken('TokenToGetPayload')->getPayload();, (*32)

The Versions

02/07 2018

dev-master

9999999-dev

A jwt package for reisssuing token purpose based on jwt-auth package.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel jwt token concurrency reissue

02/07 2018

dev-develop

dev-develop

A jwt package for reisssuing token purpose based on jwt-auth package.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel jwt token concurrency reissue

12/06 2018

0.2.6

0.2.6.0

A jwt package for reisssuing token purpose based on jwt-auth package.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel jwt token concurrency reissue

18/03 2018

0.2.5

0.2.5.0

A jwt package for reisssuing token purpose based on jwt-auth package.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel jwt token concurrency reissue

18/03 2018

0.2.4

0.2.4.0

A jwt package for reisssuing token purpose based on jwt-auth package.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel jwt token concurrency reissue

05/02 2018

0.2.3

0.2.3.0

A jwt package for reisssuing token purpose based on jwt-auth package.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel jwt token concurrency reissue

30/12 2017

0.2.2

0.2.2.0

A jwt package for reisssuing token purpose based on jwt-auth package.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel jwt token concurrency reissue

30/12 2017

0.2.1

0.2.1.0

A jwt package for reisssuing token purpose based on jwt-auth package.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel jwt token concurrency reissue

29/12 2017

0.2.0

0.2.0.0

A jwt package for reisssuing token purpose based on jwt-auth package.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel jwt token concurrency reissue

29/12 2017

0.1.9

0.1.9.0

A jwt package for reisssuing token purpose based on jwt-auth package.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel jwt token concurrency reissue

26/12 2017

0.1.8

0.1.8.0

A jwt package for reisssuing token purpose based on jwt-auth package.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel jwt token concurrency reissue

23/11 2017

0.1.7

0.1.7.0

A jwt package for reisssuing token purpose based on jwt-auth package.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel jwt token concurrency reissue

10/11 2017

0.1.5

0.1.5.0

A jwt package for reisssuing token purpose based on jwt-auth package.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel jwt token concurrency reissue

10/11 2017

0.1.3

0.1.3.0

A jwt package for reisssuing token purpose based on jwt-auth package.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel jwt token concurrency reissue

10/11 2017

0.1.4

0.1.4.0

A jwt package for reisssuing token purpose based on jwt-auth package.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel jwt token concurrency reissue

08/11 2017

0.1.1

0.1.1.0

A jwt package for reisssuing token purpose based on jwt-auth package.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel jwt token concurrency reissue

08/11 2017

0.1.2

0.1.2.0

A jwt package for reisssuing token purpose based on jwt-auth package.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel jwt token concurrency reissue

08/11 2017

0.1

0.1.0.0

A jwt package for reisssuing token purpose based on jwt-auth package.

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel jwt token concurrency reissue