Skip to content

Commit

Permalink
Simplify checkToken function.
Browse files Browse the repository at this point in the history
  • Loading branch information
maurobonfietti committed Jul 31, 2020
1 parent 7c5a0eb commit bb77bfd
Showing 1 changed file with 2 additions and 10 deletions.
12 changes: 2 additions & 10 deletions src/Middleware/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,12 @@

abstract class Base
{
private const FORBIDDEN_MESSAGE = 'Forbidden: you are not authorized.';

protected function checkToken(string $token): object
{
try {
$decoded = JWT::decode($token, getenv('SECRET_KEY'), ['HS256']);
if (is_object($decoded) && isset($decoded->sub)) {
return $decoded;
}
throw new Auth(self::FORBIDDEN_MESSAGE, 403);
return JWT::decode($token, getenv('SECRET_KEY'), ['HS256']);
} catch (\UnexpectedValueException $exception) {
throw new Auth(self::FORBIDDEN_MESSAGE, 403);
} catch (\DomainException $exception) {
throw new Auth(self::FORBIDDEN_MESSAGE, 403);
throw new Auth('Forbidden: you are not authorized.', 403);
}
}
}

0 comments on commit bb77bfd

Please sign in to comment.