File

src/auth/jwt.strategy.ts

Index

Methods

Constructor

constructor()

Methods

Async validate
validate(payload: literal type)
Parameters :
Name Type Optional
payload literal type No
Returns : unknown
import { Injectable } from "@nestjs/common";
import { PassportStrategy } from "@nestjs/passport";
import { passportJwtSecret } from "jwks-rsa";
import { ExtractJwt, Strategy } from "passport-jwt";

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor() {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      ignoreExpiration: false,
      _audience: process.env.AWS_COGNITO_CLIENT_ID,
      issuer: process.env.AWS_COGNITO_AUTHORITY,
      algorithms: ["RS256"],
      secretOrKeyProvider: passportJwtSecret({
        cache: true,
        rateLimit: true,
        jwksRequestsPerMinute: 5,
        jwksUri: process.env.AWS_COGNITO_AUTHORITY + "/.well-known/jwks.json",
      }),
    });
  }

  async validate(payload: { sub: string; email: string }) {
    return {
      idUser: payload.sub,
      email: payload.email,
      role: payload["custom:role"],
    };
  }
}

results matching ""

    No results matching ""