Enable auth-service/api-gateway on production, build their images in CI

This commit is contained in:
2026-08-18 14:11:00 +07:00
parent e5afedfa2f
commit b68005be1c
70 changed files with 6781 additions and 263 deletions
+38
View File
@@ -0,0 +1,38 @@
import {
CanActivate,
ExecutionContext,
Injectable,
UnauthorizedException,
} from "@nestjs/common";
import { JwtService } from "@nestjs/jwt";
import type { Request } from "express";
export interface JwtPayload {
sub: string;
username: string;
role: "user" | "admin";
}
function bearerToken(request: Request): string | null {
const header = request.headers.authorization;
if (!header?.startsWith("Bearer ")) return null;
return header.slice("Bearer ".length);
}
@Injectable()
export class JwtAuthGuard implements CanActivate {
constructor(private readonly jwt: JwtService) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest<Request>();
const token = bearerToken(request);
if (!token) throw new UnauthorizedException("missing bearer token");
try {
const payload = await this.jwt.verifyAsync<JwtPayload>(token);
(request as Request & { user: JwtPayload }).user = payload;
return true;
} catch {
throw new UnauthorizedException("invalid or expired token");
}
}
}