import { Injectable } from "@nestjs/common";
import { PassportStrategy } from "@nestjs/passport";
import { ExtractJwt, Strategy } from "passport-jwt";
import { ConfigService } from "@nestjs/config";

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
    constructor(configService: ConfigService) {
        super({
            jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),  // extract token from authorization header
            ignoreExpiration: false,                                  // if token is expired, return 401
            secretOrKey: configService.get("JWT_SECRET"),             // secret key
        });
    }

    async validate(payload: any) {
        return { userId: payload.sub, username: payload.username };
    }
}
