import { UnauthorizedException } from "@nestjs/common"; import { PassportStrategy } from "@nestjs/passport"; import { ExtractJwt, Strategy } from "passport-jwt"; import { IAuthStrategy } from "../../IAuthStrategy"; declare class ENTITY_NAME_INFO {} declare class ENTITY_SERVICE {} export class JwtStrategyBase extends PassportStrategy(Strategy) implements IAuthStrategy { constructor(protected readonly secretOrKey: string) { super({ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), ignoreExpiration: false, secretOrKey, }); } async validate(payload: ENTITY_NAME_INFO): Promise { const { username } = payload; const user = await this.ENTITY_SERVICE.FIND_ONE_FUNCTION({ where: { username }, }); if (!user) { throw new UnauthorizedException(); } if ( !Array.isArray(user.roles) || typeof user.roles !== "object" || user.roles === null ) { throw new Error("User roles is not a valid value"); } return { ...user, roles: user.roles as string[] }; } }