catchAdmin/catch/permissions/middleware/AuthTokenMiddleware.php

35 lines
1.0 KiB
PHP
Raw Normal View History

2020-04-29 17:37:45 +08:00
<?php
namespace catchAdmin\permissions\middleware;
2020-04-29 17:37:45 +08:00
use catcher\Code;
use catcher\exceptions\FailedException;
use thans\jwt\exception\TokenBlacklistException;
use thans\jwt\exception\TokenExpiredException;
use thans\jwt\exception\TokenInvalidException;
use thans\jwt\facade\JWTAuth;
use think\Middleware;
class AuthTokenMiddleware extends Middleware
{
public function handle($request, \Closure $next)
{
try {
JWTAuth::auth();
} catch (\Exception $e) {
if ($e instanceof TokenExpiredException) {
2020-05-18 13:42:08 +08:00
throw new FailedException('token 过期', Code::LOGIN_EXPIRED);
2020-04-29 17:37:45 +08:00
}
if ($e instanceof TokenBlacklistException) {
2020-05-19 08:22:48 +08:00
throw new FailedException('token 被加入黑名单', Code::LOGIN_BLACKLIST);
2020-04-29 17:37:45 +08:00
}
if ($e instanceof TokenInvalidException) {
throw new FailedException('token 不合法', Code::LOST_LOGIN);
}
throw new FailedException('登录用户不合法', Code::LOST_LOGIN);
}
return $next($request);
}
}