Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import * as bcrypt from 'bcryptjs'; import { decode, sign } from 'jsonwebtoken'; import { isString } from 'lodash'; import ENV from 'src/shared/env'; import { IAccessTokenPayload } from './auth.interface'; export function encodePassword(pass: string) { const salt = bcrypt.genSaltSync(12); const hash = bcrypt.hashSync(pass, salt); return hash; } export function verifyEncodedPassword(pass: string, hashed: string) { return bcrypt.compare(pass, hashed); } /** https://github.com/auth0/node-jsonwebtoken/issues/927 */ export function getAccessToken(payload: IAccessTokenPayload, expiresIn = ENV.JWT.EXPIRES_IN) { return sign(payload, ENV.JWT.SECRET, { expiresIn }); } export function decodeAccessToken(token: string): { payload?: IAccessTokenPayload; metadata?: { exp?: number; iss?: string; sub?: string; aud?: string | string[]; nbf?: number; iat?: number; jti?: string; }; } { const payload = decode(token, { complete: true })?.payload; Iif (!payload || isString(payload)) return {}; const { exp, iss, sub, aud, nbf, iat, jti, ...data } = payload; return { payload: data as IAccessTokenPayload, metadata: { exp, iss, sub, aud, nbf, iat, jti }, }; } |