Guest User

Untitled

a guest
May 3rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. // AuthHandler.js
  2.  
  3. /*
  4. * Functions
  5. */
  6.  
  7. module.exports.me = (event, context) => {
  8. context.callbackWaitsForEmptyEventLoop = false;
  9. return connectToDatabase()
  10. .then(() =>
  11. me(event.requestContext.authorizer.principalId) // the decoded.id from the VerifyToken.auth will be passed along as the principalId under the authorizer
  12. )
  13. .then(session => ({
  14. statusCode: 200,
  15. body: JSON.stringify(session)
  16. }))
  17. .catch(err => ({
  18. statusCode: err.statusCode || 500,
  19. headers: { 'Content-Type': 'text/plain' },
  20. body: { stack: err.stack, message: err.message }
  21. }));
  22. };
  23.  
  24. /*
  25. * Helpers
  26. */
  27.  
  28. function me(userId) {
  29. return User.findById(userId, { password: 0 })
  30. .then(user =>
  31. !user
  32. ? Promise.reject('No user found.')
  33. : user
  34. )
  35. .catch(err => Promise.reject(new Error(err)));
  36. }
Add Comment
Please, Sign In to add comment