Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

jwt.js 1.2KB

12345678910111213141516171819202122232425262728293031
  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. // check the h object to see if the activeSessions is accessible from it
  10. //
  11. // check useronlinestatus branch request.server.app
  12. validate: (decoded, request, h) => {
  13. // QUESTION: How can we authenticate both Session and Access Tokens here?
  14. // Always check rawAccessToken, if it fails, we check the session, if session is valid, then we reissue
  15. // if session is NOT valid, DELETE the session (and kick user back to login)
  16. // TODO: set up cron job to occassionaly clean up activeSessions
  17. const token = request.headers.authorization
  18. try {
  19. const validatedJwt = JWT.verify(token, process.env.APP_SECRET)
  20. return {
  21. isValid: true,
  22. credentials: validatedJwt.email,
  23. }
  24. } catch (err) {
  25. console.error('ERROR :=>', err)
  26. return { isValid: false, error: err.message }
  27. }
  28. },
  29. }
  30. }