選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

validate-session.js 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 validatedSessionInfo =
  31. userService.validateSession(hashedSessionToken)
  32. if (validatedSessionInfo?.email)
  33. throw new Error(
  34. `Could not validate token based on payload: ${request.payload}`,
  35. )
  36. const user = await userService.findByUserEmail(
  37. validatedSessionInfo.email,
  38. )
  39. const type = user.is_poster === 1 ? 'poster' : 'seeker'
  40. const profiles = await profileService.getCompleteProfilesFor(
  41. user.user_id,
  42. type,
  43. )
  44. // TODO: handle user with multiple profiles...
  45. const profileId = profiles[0].profile_id
  46. return {
  47. ok: true,
  48. handler: pluginConfig.handlerType,
  49. data: {
  50. ...validatedSessionInfo,
  51. profileId: profileId,
  52. },
  53. }
  54. } catch (err) {
  55. return {
  56. ok: false,
  57. handler: pluginConfig.handlerType,
  58. data: { error: err.message },
  59. }
  60. }
  61. },
  62. validate: {
  63. ...validators,
  64. failAction: 'log',
  65. },
  66. response: {
  67. schema: Joi.object({
  68. ok: Joi.bool(),
  69. handler: Joi.string(),
  70. data: Joi.object(),
  71. }).label('validate_session_res'),
  72. failAction: 'log',
  73. },
  74. },
  75. }