mirror of
https://github.com/n08i40k/schedule-parser-next.git
synced 2025-12-06 09:47:46 +03:00
Пред-деплой
This commit is contained in:
@@ -15,28 +15,31 @@ export class AuthGuard implements CanActivate {
|
||||
private readonly jwtService: JwtService,
|
||||
) {}
|
||||
|
||||
public static extractTokenFromRequest(req: Request): string | null {
|
||||
public static extractTokenFromRequest(req: Request): string {
|
||||
const [type, token] = req.headers.authorization?.split(" ") ?? [];
|
||||
return type === "Bearer" ? token : null;
|
||||
|
||||
if (type !== "Bearer" || !token || token.length === 0)
|
||||
throw new UnauthorizedException("Не указан токен!");
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const request = context.switchToHttp().getRequest();
|
||||
const token = AuthGuard.extractTokenFromRequest(request);
|
||||
|
||||
if (!token) throw new UnauthorizedException("Не указан токен!");
|
||||
|
||||
try {
|
||||
if (
|
||||
!(await this.jwtService.verifyAsync(token)) ||
|
||||
!(await this.usersService.contains({ accessToken: token }))
|
||||
) {
|
||||
// noinspection ExceptionCaughtLocallyJS
|
||||
throw new Error();
|
||||
if (!token)
|
||||
try {
|
||||
if (
|
||||
!(await this.jwtService.verifyAsync(token)) ||
|
||||
!(await this.usersService.contains({ accessToken: token }))
|
||||
) {
|
||||
// noinspection ExceptionCaughtLocallyJS
|
||||
throw new Error();
|
||||
}
|
||||
} catch {
|
||||
throw new UnauthorizedException("Указан неверный токен!");
|
||||
}
|
||||
} catch {
|
||||
throw new UnauthorizedException("Указан неверный токен!");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user