UNPKG

node-express-mongodb-jwt-rest-api-skeleton

Version:

Node.js express.js MongoDB JWT REST API - This is a basic API REST skeleton written on JavaScript using async/await. Great for building a starter web API for your front-end (Android, iOS, Vue, react, angular, or anything that can consume an API)

27 lines (22 loc) 677 B
const crypto = require('crypto') const secret = process.env.JWT_SECRET const algorithm = 'aes-256-cbc' // Key length is dependent on the algorithm. In this case for aes256, it is // 32 bytes (256 bits). const key = crypto.scryptSync(secret, 'salt', 32) const iv = Buffer.alloc(16, 0) // Initialization crypto vector /** * Decrypts text * @param {string} text - text to decrypt */ const decrypt = (text = '') => { const decipher = crypto.createDecipheriv(algorithm, key, iv) try { let decrypted = decipher.update(text, 'hex', 'utf8') decrypted += decipher.final('utf8') return decrypted } catch (err) { return err } } module.exports = { decrypt }