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.

validatejwt.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict'
  2. const Joi = require('joi')
  3. const pluginConfig = {
  4. handlerType: 'jwt',
  5. docs: {
  6. get: {
  7. description: 'validates jwt for each step of survey',
  8. notes: 'validates jwt for each step of survey',
  9. },
  10. },
  11. }
  12. module.exports = {
  13. method: 'GET',
  14. path: '/validatejwt/{jwt}',
  15. options: {
  16. ...pluginConfig.docs.get,
  17. tags: ['api'],
  18. auth: false,
  19. cors: true,
  20. handler: async function (request, h) {
  21. const jwt = request.params.jwt
  22. const { userService } = request.server.services()
  23. const jwtIsValid = userService.validateToken(jwt)
  24. console.log('jwtIsValid :=>', jwtIsValid)
  25. try {
  26. return {
  27. ok: true,
  28. handler: pluginConfig.handlerType,
  29. data: { isValid: jwtIsValid.isValid },
  30. }
  31. } catch (err) {
  32. return {
  33. ok: false,
  34. handler: pluginConfig.handlerType,
  35. data: {
  36. error: err,
  37. },
  38. }
  39. }
  40. },
  41. validate: {
  42. failAction: 'log',
  43. },
  44. response: {
  45. schema: Joi.object({
  46. ok: Joi.bool(),
  47. handler: Joi.string(),
  48. data: Joi.object(),
  49. }).label('validate_jwt_res'),
  50. failAction: 'log',
  51. },
  52. },
  53. }