Files
calendar/apps/api/v2/src/modules/jwt/jwt.service.ts
T
2025-03-05 14:29:11 +00:00

32 lines
919 B
TypeScript

import { Injectable } from "@nestjs/common";
import { JwtService as NestJwtService } from "@nestjs/jwt";
@Injectable()
export class JwtService {
constructor(private readonly nestJwtService: NestJwtService) {}
signAccessToken(payload: Payload) {
const accessToken = this.sign({ type: "access_token", ...payload });
return accessToken;
}
signRefreshToken(payload: Payload) {
const refreshToken = this.sign({ type: "refresh_token", ...payload });
return refreshToken;
}
sign(payload: Payload) {
const issuedAtTime = this.getIssuedAtTime();
const token = this.nestJwtService.sign({ ...payload, iat: issuedAtTime });
return token;
}
getIssuedAtTime() {
// divided by 1000 because iat (issued at time) is in seconds (not milliseconds) as informed by JWT specification
return Math.floor(Date.now() / 1000);
}
}
type Payload = Record<string | number | symbol, any>;