Skip to content

Instantly share code, notes, and snippets.

@carboleda
Last active February 23, 2019 15:05
Show Gist options
  • Save carboleda/9ac8f0c98971b047ee5944c926213194 to your computer and use it in GitHub Desktop.
Save carboleda/9ac8f0c98971b047ee5944c926213194 to your computer and use it in GitHub Desktop.
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
}
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