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.

getsession.js 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict'
  2. const Joi = require('joi')
  3. const pluginConfig = {
  4. handlerType: 'authentication',
  5. docs: {
  6. get: {
  7. description: 'gets session token for authentication',
  8. notes: 'Gets session token for authentication',
  9. },
  10. },
  11. }
  12. module.exports = {
  13. method: 'POST',
  14. path: '/getsession',
  15. options: {
  16. ...pluginConfig.docs.get,
  17. tags: ['api'],
  18. auth: false,
  19. cors: {
  20. headers: ['Authorization', 'Content-Type'],
  21. exposedHeaders: ['Authorization', 'Access-Control-Expose-Headers'],
  22. },
  23. handler: async function (request, h) {
  24. const { userService } = request.server.services()
  25. const res = request.payload
  26. const token = await userService.createToken(res)
  27. try {
  28. const response = h.response({
  29. ok: true,
  30. handler: pluginConfig.handlerType,
  31. data: token,
  32. })
  33. response.header('Authorization', token)
  34. return response
  35. } catch (err) {
  36. return {
  37. ok: false,
  38. handler: pluginConfig.handlerType,
  39. data: {
  40. error: err,
  41. },
  42. }
  43. }
  44. },
  45. validate: {
  46. failAction: 'log',
  47. },
  48. response: {
  49. // TODO: change back to accommodate new h.response return values
  50. schema: Joi.any().label('get_session_res'),
  51. failAction: 'log',
  52. },
  53. },
  54. }