2017 © Pedro Peláez
 

library laravel-jwt

jwt auth for encode decode and token expire_in authenticate

image

jerrygaoyang/laravel-jwt

jwt auth for encode decode and token expire_in authenticate

  • Tuesday, November 21, 2017
  • by jerrygaoyang
  • Repository
  • 0 Watchers
  • 3 Stars
  • 181 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 65 % Grown

The README.md

laravel-jwt

Installation

  • First: Require this package with composer using the following command

composer require jerrygaoyang/laravel-jwt, (*1)

  • Second: add the service provider to the providers array in config/app.php

Jerry\JWT\Providers\JWTProvider::class, (*2)

  • Last: publish jwt config to laravel config path

php artisan vendor:publish --provider="Jerry\JWT\Providers\JWTProvider", (*3)

Configuration

  • add the JWT middleware to the routeMiddleware array in app/Http/Kenel.php

'jwt' => \Jerry\JWT\Middleware\JWTMiddleware::class,, (*4)

  • JWT config in config/jwt.php
<?php
/**
 * Created by PhpStorm.
 * User: gaoyang
 * Date: 2017/11/14
 * Time: 9:32
 */

return [
    "secret" => "PIe5T3xJWAMA95Uwf7pde7gmS7ZTiURg",   //jwt SHA256 signature use the secret
    "expire_in" => 604800,                            //jwt expire_in the times(seconds) , default 604800(a week)
];

User Guide

encode and decode

use Jerry/JWT/JWT;

$payload = [
  "user_id" => 1
];

$token = JWT::encode($payload);
print_r($token);

echo "<br>";

$payload = JWT::decode($token);
print_r($payload);

http api process for laravel  

  • First: http request must have header
{
    "Authorization": "jwt PIe5T3xJWAMA95Uwf7pde7gmS7ZTiURg"
}   
  • Second: laravel route use middleware jwt
Route::middleware(['jwt'])->group(function () {
    Route::get('/test2', 'TestController@test2');
});
  • Last: user request get jwt decode payload in your laravel controller
$payload = $request->get('jwt');

Exception (global for api)

you can copy below code to your Laravel app/Exceptions/handler.php render function;, (*5)

it's easy to change the token exception for us;, (*6)

it's easy to change the return data for api response., (*7)

of course, we should:, (*8)

use Jerry\JWT\Exceptions\TokenFormatException;, (*9)

use Jerry\JWT\Exceptions\TokenExpiredException;, (*10)

use Jerry\JWT\Exceptions\TokenForwardException;, (*11)

use Jerry\JWT\Exceptions\PayloadFormatException;, (*12)

    if ($exception instanceof TokenFormatException) {
            return response()->json([
                'code' => $exception->getCode(),
                'message' => $exception->getMessage(),
                'data' => ''
            ]);
        }
        if ($exception instanceof TokenExpiredException) {
            return response()->json([
                'code' => $exception->getCode(),
                'message' => $exception->getMessage(),
                'data' => ''
            ]);
        }
        if ($exception instanceof TokenForwardException) {
            return response()->json([
                'code' => $exception->getCode(),
                'message' => $exception->getMessage(),
                'data' => ''
            ]);
        }
        if ($exception instanceof PayloadFormatException) {
            return response()->json([
                'code' => $exception->getCode(),
                'message' => $exception->getMessage(),
                'data' => ''
            ]);
        }

The Versions

21/11 2017

v1.0.0.x-dev

1.0.0.9999999-dev

jwt auth for encode decode and token expire_in authenticate

  Sources   Download

MIT

by gaoyang

21/11 2017

v1.0.5

1.0.5.0

jwt auth for encode decode and token expire_in authenticate

  Sources   Download

MIT

by gaoyang

18/11 2017

v1.0.4

1.0.4.0

jwt auth for encode decode and token expire_in authenticate

  Sources   Download

MIT

by gaoyang