26 lines
570 B
JavaScript
26 lines
570 B
JavaScript
|
var jwt = require('jsonwebtoken');
|
||
|
|
||
|
let Auth = {}
|
||
|
|
||
|
const secretKey = '@TODO define secret constant its important!!!'
|
||
|
|
||
|
Auth.createToken = (userId) => {
|
||
|
const signedData = {'id': userId, 'date':Date.now()}
|
||
|
const token = jwt.sign(signedData, secretKey)
|
||
|
return token
|
||
|
}
|
||
|
Auth.decodeToken = (token) => {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
jwt.verify(token, secretKey, function(err, decoded){
|
||
|
if(err || decoded.id == undefined){
|
||
|
reject('Bad Token')
|
||
|
return
|
||
|
}
|
||
|
//Pass back decoded token
|
||
|
resolve(decoded)
|
||
|
return
|
||
|
});
|
||
|
})
|
||
|
}
|
||
|
|
||
|
module.exports = Auth
|