Override jwt.auth middleware

This commit is contained in:
Manish Verma
2018-08-10 14:51:43 +05:30
parent 88483e5ae6
commit 91cf657c95
3 changed files with 69 additions and 19 deletions

View File

@@ -59,7 +59,7 @@ class ApiController extends Controller
{
$this->request = $request;
$this->middleware('jwt.auth');
$this->middleware('jwt.authOveride');
$this->middleware('api', ['except' => 'GenerateApiKey']);
try {

View File

@@ -48,23 +48,24 @@ class Kernel extends HttpKernel
* @var array
*/
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'roles' => \App\Http\Middleware\CheckRole::class,
'role.agent' => \App\Http\Middleware\CheckRoleAgent::class,
'role.user' => \App\Http\Middleware\CheckRoleUser::class,
'api' => \App\Http\Middleware\ApiKey::class,
'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class,
'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class,
'update' => \App\Http\Middleware\CheckUpdate::class,
'board' => \App\Http\Middleware\CheckBoard::class,
'install' => \App\Http\Middleware\Install::class,
'redirect' => \App\Http\Middleware\Redirect::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'installer' => \App\Http\Middleware\IsInstalled::class,
'force.option' => \App\Http\Middleware\TicketViewURL::class,
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'roles' => \App\Http\Middleware\CheckRole::class,
'role.agent' => \App\Http\Middleware\CheckRoleAgent::class,
'role.user' => \App\Http\Middleware\CheckRoleUser::class,
'api' => \App\Http\Middleware\ApiKey::class,
'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class,
'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class,
'jwt.authOveride' => \App\Http\Middleware\JwtAuthenticate::class,
'update' => \App\Http\Middleware\CheckUpdate::class,
'board' => \App\Http\Middleware\CheckBoard::class,
'install' => \App\Http\Middleware\Install::class,
'redirect' => \App\Http\Middleware\Redirect::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'installer' => \App\Http\Middleware\IsInstalled::class,
'force.option' => \App\Http\Middleware\TicketViewURL::class,
];
}

View File

@@ -0,0 +1,49 @@
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Http\Middleware;
use Closure;
use Tymon\JWTAuth\Http\Middleware\BaseMiddleware;
/**
* Middleware to handle JWT Authentication for the API call which requires
* a valid token
*
* @author Manish Verma <manish.verma@ladybirdweb.com>
* @since v1.10
*/
class JwtAuthenticate extends BaseMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
*
* @return mixed
*/
public function handle($request, Closure $next)
{
try {
$this->authenticate($request);
return $next($request);
} catch (\Exception $e) {
return response(
['success' => false, 'message' => $e->getMessage()],
$e->getStatusCode()
);
}
}
}