This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/src/jwt.js

33 lines
934 B
JavaScript

import jwt from 'jsonwebtoken';
import fs from 'fs';
class Jwt {
constructor(privateKey, publicKey) {
this.privateKey = fs.readFileSync(privateKey);
this.publicKey = fs.readFileSync(publicKey);
}
sign(payload, expiresIn = '365d') {
return jwt.sign(payload, this.privateKey, {
expiresIn,
algorithm: 'RS256',
audience: process.env.ALL_LOCALES_URLS.split(','),
issuer: process.env.BASE_URL,
});
}
validate(token) {
try {
return jwt.verify(token, this.publicKey, {
algorithm: 'RS256',
audience: process.env.ALL_LOCALES_URLS.split(','),
issuer: process.env.ALL_LOCALES_URLS.split(','),
});
} catch (e) {
console.error(e);
}
}
}
export default new Jwt(__dirname + '/../keys/private.pem', __dirname + '/../keys/public.pem');