You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
957 B
39 lines
957 B
<?php |
|
|
|
namespace App\Http\Middleware; |
|
|
|
use App\Constants\HttpStatus; |
|
use Closure; |
|
use Firebase\JWT\JWT; |
|
use Firebase\JWT\Key; |
|
use Illuminate\Http\Request; |
|
|
|
class JwtMiddleware |
|
{ |
|
/** |
|
* Manejar una solicitud entrante. |
|
* |
|
* @return mixed |
|
*/ |
|
public function handle(Request $request, Closure $next) |
|
{ |
|
$token = $request->bearerToken(); |
|
|
|
if (! $token) { |
|
return response()->json(['message' => trans('api.SIN_TOKEN')], HttpStatus::BAD_REQUEST); |
|
} |
|
|
|
try { |
|
|
|
JWT::decode($token, new Key(config('app.jwt_secret'), 'HS256')); |
|
|
|
return $next($request); |
|
|
|
} catch (\Firebase\JWT\ExpiredException $e) { |
|
return response()->json(['message' => trans('api.TOKEN_EXPIRADO')], HttpStatus::UNAUTHORIZED); |
|
|
|
} catch (\Exception $e) { |
|
return response()->json(['message' => trans('api.TOKEN_INVALIDO')], HttpStatus::UNAUTHORIZED); |
|
} |
|
} |
|
}
|
|
|