lbx-jwt
Version:
Provides JWT authentication for loopback applications. Includes storing roles inside tokens and handling refreshing. Built-in reuse detection.
46 lines • 1.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.JwtUtilities = void 0;
const jsonwebtoken_1 = require("jsonwebtoken");
/**
* Encapsulates functionality of the jsonwebtoken package.
*/
class JwtUtilities {
/**
* Asynchronously sign the given payload into a JSON Web Token string payload.
* @param payload - Any info that should be put inside the token.
* @param secret - The secret used to encrypt the token.
* @param options - Additional options like "expiresIn".
* @returns A promise of the jwt.
*/
static async signAsync(payload, secret, options) {
return new Promise((resolve, reject) => {
try {
const jwtValue = (0, jsonwebtoken_1.sign)(payload, secret, options);
resolve(jwtValue);
}
catch (error) {
reject(error);
}
});
}
/**
* Asynchronously verify given token using a secret or a public key to get a decoded token.
* @param token - The token to encode.
* @param secret - The secret to encode the token with.
* @returns The encoded token.
*/
static async verifyAsync(token, secret) {
return new Promise((resolve, reject) => {
try {
const jwt = (0, jsonwebtoken_1.verify)(token, secret, { complete: true });
resolve(jwt);
}
catch (error) {
reject(error);
}
});
}
}
exports.JwtUtilities = JwtUtilities;
//# sourceMappingURL=jwt.utilities.js.map