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.

validatesession.js 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict'
  2. const { plugin } = require('@hapi/inert')
  3. const Joi = require('joi')
  4. const pluginConfig = {
  5. handlerType: 'jwt',
  6. docs: {
  7. get: {
  8. description: 'validates session token for each step of survey',
  9. notes: 'validates session token for each step of survey',
  10. },
  11. },
  12. }
  13. module.exports = {
  14. method: 'POST',
  15. path: '/validatesession',
  16. options: {
  17. ...pluginConfig.docs.get,
  18. tags: ['api'],
  19. auth: false,
  20. cors: {
  21. headers: ['Authorization', 'Content-Type'],
  22. exposedHeaders: ['Authorization', 'Access-Control-Expose-Headers'],
  23. },
  24. handler: async function (request, h) {
  25. const hashedSessionToken = request.payload
  26. const { userService } = request.server.services()
  27. try {
  28. const validatedSessionToken =
  29. userService.validateSession(hashedSessionToken)
  30. return {
  31. ok: true,
  32. handler: pluginConfig.handlerType,
  33. data: validatedSessionToken,
  34. }
  35. } catch (err) {
  36. return {
  37. ok: false,
  38. handler: pluginConfig.handlerType,
  39. data: { error: err.message },
  40. }
  41. }
  42. },
  43. validate: {
  44. failAction: 'log',
  45. },
  46. response: {
  47. schema: Joi.object({
  48. ok: Joi.bool(),
  49. handler: Joi.string(),
  50. data: Joi.object(),
  51. }).label('validate_session_res'),
  52. failAction: 'log',
  53. },
  54. },
  55. }