dff-util
Version:
DesignForFeature Utilities
36 lines (35 loc) • 1.73 kB
JavaScript
;
// bun run src/tests/jwt.ts
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("../index");
const runJwtExamples = () => __awaiter(void 0, void 0, void 0, function* () {
const secret = 'your-secret-key';
const payload = { userId: '123', role: 'admin' };
// 1. Encode a JWT
const token = yield (0, index_1.JwtEncode)(payload, secret, 3600);
console.log('Encoded JWT Token:', token);
// 2. Verify the JWT
try {
const verifiedPayload = yield (0, index_1.JwtVerify)(token, secret);
console.log('Verified Payload:', verifiedPayload);
}
catch (error) {
console.error('Verification Error:', error.message);
}
// 3. Decode the JWT without verification
const decodedPayload = (0, index_1.JwtDecode)(token);
console.log('Decoded Payload (without verification):', decodedPayload);
// 4. Check if the JWT is valid
const isValid = (0, index_1.JwtValid)(token);
console.log('Is JWT valid:', isValid);
});
runJwtExamples();