2019-07-19 13:51:57 -07:00
|
|
|
var jwt = require('jsonwebtoken');
|
|
|
|
|
|
|
|
let Auth = {}
|
|
|
|
|
2020-04-13 00:44:57 -07:00
|
|
|
const tokenSecretKey = process.env.JSON_KEY
|
2019-07-19 13:51:57 -07:00
|
|
|
|
2020-05-06 00:10:27 -07:00
|
|
|
Auth.createToken = (userId, masterKey) => {
|
|
|
|
const signedData = {'id':userId, 'date':Date.now(), 'masterKey':masterKey}
|
2020-04-13 00:44:57 -07:00
|
|
|
const token = jwt.sign(signedData, tokenSecretKey)
|
2019-07-19 13:51:57 -07:00
|
|
|
return token
|
|
|
|
}
|
|
|
|
Auth.decodeToken = (token) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-04-13 00:44:57 -07:00
|
|
|
jwt.verify(token, tokenSecretKey, function(err, decoded){
|
2019-07-19 13:51:57 -07:00
|
|
|
if(err || decoded.id == undefined){
|
|
|
|
reject('Bad Token')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
//Pass back decoded token
|
|
|
|
resolve(decoded)
|
|
|
|
return
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Auth
|