SolidScribe/server/helpers/Auth.js

26 lines
591 B
JavaScript
Raw Normal View History

var jwt = require('jsonwebtoken');
let Auth = {}
const tokenSecretKey = process.env.JSON_KEY
Auth.createToken = (userId, masterKey) => {
const signedData = {'id':userId, 'date':Date.now(), 'masterKey':masterKey}
const token = jwt.sign(signedData, tokenSecretKey)
return token
}
Auth.decodeToken = (token) => {
return new Promise((resolve, reject) => {
jwt.verify(token, tokenSecretKey, function(err, decoded){
if(err || decoded.id == undefined){
reject('Bad Token')
return
}
//Pass back decoded token
resolve(decoded)
return
});
})
}
module.exports = Auth