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.

getaccess.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict'
  2. const Joi = require('joi')
  3. const pluginConfig = {
  4. handlerType: 'authentication',
  5. docs: {
  6. get: {
  7. description: 'gets access token for authentication',
  8. notes: 'Gets access token for authentication',
  9. },
  10. },
  11. }
  12. module.exports = {
  13. method: 'POST',
  14. path: '/getaccess',
  15. options: {
  16. ...pluginConfig.docs.get,
  17. tags: ['api'],
  18. auth: false,
  19. cors: {
  20. headers: ['Authorization'],
  21. exposedHeaders: ['Authorization', 'Access-Control-Expose-Headers'],
  22. },
  23. handler: async function (request, h) {
  24. const { userService } = request.server.services()
  25. const hash = request.payload.hash
  26. const accessToken = await userService.createToken({
  27. ...hash,
  28. // NOTE: Set Expiration Time for Access Token Here
  29. // expires: 60 * 2,
  30. // TESTING:
  31. expires: 30,
  32. })
  33. userService.activeSessions[`${hash}`].accessToken = accessToken
  34. const accessTokenInHashedSessions =
  35. userService.activeSessions[`${hash}`].accessToken ===
  36. accessToken
  37. ? true
  38. : false
  39. // TODO: instead of putting the token in the return headers,
  40. // simply put it in the activeSessions Object
  41. try {
  42. const response = h.response({
  43. ok: true,
  44. handler: pluginConfig.handlerType,
  45. data: accessTokenInHashedSessions,
  46. })
  47. // response.header('Authorization', token)
  48. return response
  49. } catch (err) {
  50. return {
  51. ok: false,
  52. handler: pluginConfig.handlerType,
  53. data: {
  54. error: err,
  55. },
  56. }
  57. }
  58. },
  59. validate: {
  60. failAction: 'log',
  61. },
  62. response: {
  63. // TODO: change back to accommodate new h.response return values
  64. schema: Joi.any().label('get_access_res'),
  65. failAction: 'log',
  66. },
  67. },
  68. }