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.

validate-session.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. 'use strict'
  2. const Joi = require('joi')
  3. const pluginConfig = {
  4. handlerType: 'jwt',
  5. docs: {
  6. get: {
  7. description: 'validates session token for each step of survey',
  8. notes: 'Validates session token for each step of survey',
  9. },
  10. },
  11. }
  12. const validators = {
  13. payload: Joi.string(),
  14. }
  15. module.exports = {
  16. method: 'POST',
  17. path: '/validate-session',
  18. options: {
  19. ...pluginConfig.docs.get,
  20. tags: ['api'],
  21. auth: false,
  22. cors: {
  23. headers: ['Authorization', 'Content-Type'],
  24. exposedHeaders: ['Authorization', 'Access-Control-Expose-Headers'],
  25. },
  26. handler: async function (request, h) {
  27. const hashedSessionToken = request.payload
  28. const { userService, profileService } = request.server.services()
  29. try {
  30. const validatedSessionToken =
  31. userService.validateSession(hashedSessionToken)
  32. const user = await userService.findByUserEmail(
  33. validatedSessionToken.email,
  34. )
  35. const type = user.is_poster === 1 ? 'poster' : 'seeker'
  36. const profiles = await profileService.getCompleteProfilesFor(
  37. user.user_id,
  38. type,
  39. )
  40. // TODO: handle user with multiple profiles...
  41. const profileId = profiles[0].profile_id
  42. return {
  43. ok: true,
  44. handler: pluginConfig.handlerType,
  45. data: {
  46. ...validatedSessionToken,
  47. profileId: profileId,
  48. },
  49. }
  50. } catch (err) {
  51. return {
  52. ok: false,
  53. handler: pluginConfig.handlerType,
  54. data: { error: err.message },
  55. }
  56. }
  57. },
  58. validate: {
  59. ...validators,
  60. failAction: 'log',
  61. },
  62. response: {
  63. schema: Joi.object({
  64. ok: Joi.bool(),
  65. handler: Joi.string(),
  66. data: Joi.object(),
  67. }).label('validate_session_res'),
  68. failAction: 'log',
  69. },
  70. },
  71. }