import jwt from 'jsonwebtoken' ; import { Guard } from '@server/shared/src/core/logic/Guard' ; import { Result } from '@server/shared/src/core/logic/Result' ; import authConfig from '@config/auth' ; interface IJWTProps { sub : string ; } interface IJWTPayload { name : string ; email : string ; } export class JWT { public sub : string ; public token : string ; private constructor ( props ) { this . sub = props . sub ; } private static signJwt ( props , payload ) { return jwt . sign ( payload , authConfig . secret , { subject : props . sub , expiresIn : authConfig . tokenExpiryTimeInSeconds , } ) ; } public static create ( props , payload ) { const guardResult = Guard . againstNullOrUndefined ( props . sub , 'sub' ) ; if ( ! guardResult . succeeded ) { return Result . fail ( guardResult . message ) ; } const signedToken = this . signJwt ( props , payload ) ; const jwtToken = new JWT ( props ) ; jwtToken . token = signedToken ; return Result . ok ( jwtToken ) ; } }