UNPKG

@yash112/jwt-library

Version:

Encode, Decode & Validate JSON Web Token (JWTs)

40 lines (39 loc) 1.33 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.encode_jwt = encode_jwt; exports.decode_jwt = decode_jwt; exports.validate_jwt = validate_jwt; const jwt_1 = require("./jwt"); function encode_jwt(secret, id, payload, ttl) { const expiresIn = ttl ? { expiresIn: ttl } : {}; return (0, jwt_1.sign)(Object.assign({ id }, payload), secret, expiresIn); } function decode_jwt(secret, token) { const decoded = (0, jwt_1.verify)(token, secret); const { id, exp } = decoded, payload = __rest(decoded, ["id", "exp"]); return { id: String(id), payload, expires_at: exp ? new Date(exp * 1000) : new Date(0) }; } function validate_jwt(secret, token) { try { decode_jwt(secret, token); return true; } catch (_a) { return false; } }