Last active
February 23, 2019 15:05
-
-
Save carboleda/9ac8f0c98971b047ee5944c926213194 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const jsonwebtoken = require('jsonwebtoken'); | |
const secretOrPrivateKey = 'devhack.com'; | |
function verifyToken(token) { | |
console.log('verifyToken.token', token); | |
if (token && token.length > 7 && token.substring(0, 7).toLowerCase() === 'bearer ') { | |
token = token.slice(7); | |
} | |
const data = jsonwebtoken.verify(token, secretOrPrivateKey); | |
console.log('verifyToken.data', data); | |
return data; | |
} | |
module.exports = { | |
verifyToken | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
seneca.add('role:users,cmd:getAll,authorization:*', async (payload, reply) => { | |
try { | |
const usersRef = await db.collection('users').get(); | |
const users = usersRef.docs.map(user => user.data()); | |
reply(null, users); | |
} catch(error) { | |
reply(error); | |
} | |
}); | |
//Esta funcion debe definirse despues de todos los actions | |
seneca.wrap('role:users,cmd:*,authorization:*', function(payload, reply) { | |
if(!payload.authorization || !utitlies.verifyToken(payload.authorization)) { | |
return reply(new Error('Invalid token')); | |
} | |
this.prior(payload, reply); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment