bc-node-sdk
Version:
BetterCommerce's NodeJS SDK encapsulates the base framework for all the Next.js applications.
41 lines (40 loc) • 1.36 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// Package Imports
const jwt_decode_1 = __importDefault(require("jwt-decode"));
/**
* Class {@link TokenUtil} encapsulates utility functions to work with JWT tokens.
*/
class TokenUtil {
/**
* Decodes a JWT token and returns the payload as a JSON object
* @param token the JWT token to decode
* @returns the payload of the JWT token
*/
static decodeToken(token) {
var value = (0, jwt_decode_1.default)(token);
return value;
}
;
/**
* Determines if a JWT token is valid or not
* @param value the JWT token to check
* @returns true if the token is valid, false otherwise
*/
static isValidToken(value) {
if (value) {
const tokenValue = this.decodeToken(value);
if (tokenValue && tokenValue.exp) {
const dtExpiry = new Date(tokenValue.exp * 1000);
const dtNow = new Date();
const isValid = dtNow.getTime() < dtExpiry.getTime();
return isValid;
}
}
return false;
}
}
exports.default = TokenUtil;