@selfcommunity/api-services
Version:
Client api for SelfCommunity.
52 lines (51 loc) • 1.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseJwt = exports.generateJWTToken = void 0;
const tslib_1 = require("tslib");
const jose = tslib_1.__importStar(require("jose"));
/**
* Generate a JWT
:::tip Context can be consumed as following:
```jsx
1. const token = await generateJWTToken(userId, secretKey, expirationTime);
```
```jsx
2. generateJWTToken(userId, secretKey, expirationTime).then(token => {...});
```
:::
* @param userId
* @param secretKey
* @param expirationTime
*/
function generateJWTToken(userId, secretKey, expirationTime = '2h') {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
let data = {
token_type: 'access',
user_id: userId,
jti: new Date().getTime().toString()
};
const privateKey = new TextEncoder().encode(secretKey);
return yield new jose.SignJWT(data)
.setProtectedHeader({ alg: 'HS256', typ: 'JWT' })
.setIssuedAt()
.setExpirationTime(expirationTime ? expirationTime : '2h')
.sign(privateKey);
});
}
exports.generateJWTToken = generateJWTToken;
/**
* Extract from a jwt token payload
* @param token
*/
function parseJwt(token) {
let base64Url = token.split('.')[1];
let base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
let jsonPayload = decodeURIComponent(atob(base64)
.split('')
.map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
})
.join(''));
return JSON.parse(jsonPayload);
}
exports.parseJwt = parseJwt;