Guest User

Untitled

a guest
May 26th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. const jwt = require('jsonwebtoken')
  2.  
  3. function getUserId(ctx) {
  4. let authorization = ctx.request ? ctx.request.get('authorization') : null
  5.  
  6. // Get from subscription connection context
  7. if (!authorization) {
  8. authorization = ctx.connection ? ctx.connection.context.authorization : null
  9. }
  10.  
  11. if (authorization) {
  12. const token = authorization.replace('Bearer ', '')
  13. const { userId } = jwt.verify(token, process.env.APP_SECRET)
  14. return userId
  15. }
  16.  
  17. throw new AuthError()
  18. }
  19.  
  20. class AuthError extends Error {
  21. constructor() {
  22. super('Not authorized')
  23. }
  24. }
  25.  
  26. module.exports = {
  27. getUserId,
  28. AuthError
  29. }
Add Comment
Please, Sign In to add comment