You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jwt.js 1.1KB

123456789101112131415161718192021222324252627282930
  1. 'use strict'
  2. const JWT = require('jsonwebtoken')
  3. module.exports = options => {
  4. return {
  5. key: options.jwtKey,
  6. verifyOptions: {
  7. algorithms: ['HS256'],
  8. },
  9. // TODO: check the h object to see if the activeSessions is accessible from it
  10. // check useronlinestatus branch request.server.app
  11. // Always check rawAccessToken, if it fails, we check the session, if session
  12. // is valid, then we reissue it
  13. // if session is NOT valid, DELETE the session (and kick user back to login)
  14. // TODO: set up cron job to occassionaly clean up activeSessions
  15. validate: (decoded, request, h) => {
  16. const token = request.headers.authorization
  17. try {
  18. const validatedJwt = JWT.verify(token, process.env.APP_SECRET)
  19. return {
  20. isValid: true,
  21. credentials: validatedJwt.email,
  22. }
  23. } catch (err) {
  24. console.error('ERROR :=>', err)
  25. return { isValid: false, error: err.message }
  26. }
  27. },
  28. }
  29. }