Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 'use strict'
  2. const Joi = require('joi')
  3. const errorSchema = require('../../schemas/errors')
  4. const userSchema = require('../../schemas/user')
  5. const pluginConfig = {
  6. handlerType: 'user',
  7. docs: {
  8. description: 'login',
  9. notes: 'Attempt login',
  10. },
  11. opts: {
  12. tags: ['api'],
  13. auth: false,
  14. cors: true,
  15. },
  16. cookieOpts: {
  17. ttl: 1 * 24 * 60 * 60 * 1000,
  18. encoding: 'none',
  19. isHttpOnly: true,
  20. clearInvalid: true,
  21. strictHeader: true,
  22. isSecure: true,
  23. },
  24. }
  25. /** Validator functions by request method */
  26. const validators = {
  27. post: {
  28. payload: Joi.object({
  29. user_email: Joi.string(),
  30. password: Joi.string(),
  31. }),
  32. },
  33. user: userSchema.single,
  34. error: errorSchema.single,
  35. }
  36. module.exports = {
  37. method: 'POST',
  38. path: '/login',
  39. options: {
  40. ...pluginConfig.docs,
  41. ...pluginConfig.opts,
  42. handler: async function (request, h) {
  43. try {
  44. const { userService, profileService } =
  45. request.server.services()
  46. const res = request.payload
  47. // Callback to use as transaction
  48. const login = async txn => {
  49. return await userService.login(
  50. {
  51. email: res.user_email,
  52. password: res.password,
  53. },
  54. txn,
  55. )
  56. }
  57. // Bound context from your plugin server declaration
  58. const userAuth = await h.context.transaction(login)
  59. const token = userService.createToken(userAuth)
  60. const response = h.response('success')
  61. const email = userAuth.user_email
  62. const user = await userService.findByEmail(email)
  63. const type = user.is_poster == 1 ? 'poster' : 'seeker'
  64. const profiles = await profileService.getCompleteProfilesFor(
  65. user.user_id,
  66. type,
  67. )
  68. return {
  69. ok: true,
  70. handler: pluginConfig.handlerType,
  71. data: {
  72. profiles,
  73. user_email: user.user_email,
  74. jwtToken: token,
  75. },
  76. }
  77. } catch (err) {
  78. console.error(err)
  79. return {
  80. ok: false,
  81. handler: pluginConfig.handlerType,
  82. data: { error: `${err}` },
  83. }
  84. }
  85. },
  86. validate: validators.post,
  87. response: {
  88. status: {
  89. 201: Joi.object({
  90. ok: Joi.bool(),
  91. handler: Joi.string(),
  92. data: Joi.object({
  93. user_email: Joi.string(),
  94. jwtToken: Joi.string(),
  95. profiles: Joi.array(),
  96. }),
  97. }).label('login_res'),
  98. 409: Joi.object({
  99. ok: Joi.bool(),
  100. handler: Joi.string(),
  101. data: validators.error,
  102. }).label('login_error'),
  103. },
  104. },
  105. },
  106. }