SolidScribe/server/helpers/Auth.js

26 lines
558 B
JavaScript
Raw Normal View History

var jwt = require('jsonwebtoken');
let Auth = {}
const tokenSecretKey = process.env.JSON_KEY
Auth.createToken = (userId) => {
const signedData = {'id': userId, 'date':Date.now()}
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