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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. }
  12. /** Validator functions by request method */
  13. const validators = {
  14. post: {
  15. payload: Joi.object({
  16. user_email: Joi.string(),
  17. password: Joi.string(),
  18. }),
  19. },
  20. user: userSchema.single,
  21. error: errorSchema.single,
  22. }
  23. module.exports = {
  24. method: 'POST',
  25. path: '/login',
  26. options: {
  27. ...pluginConfig.docs,
  28. tags: ['api'],
  29. auth: false,
  30. handler: async function (request, h) {
  31. try {
  32. const { userService, profileService } =
  33. request.server.services()
  34. const res = request.payload
  35. // Callback to use as transaction
  36. const login = async txn => {
  37. return await userService.login(
  38. {
  39. email: res.user_email,
  40. password: res.password,
  41. },
  42. txn,
  43. )
  44. }
  45. // Bound context from your plugin server declaration
  46. const user = await h.context.transaction(login)
  47. const rawUser = await userService.findByUserEmail(
  48. res.user_email,
  49. )
  50. // Uses Same Logic Behind Initial Sign Up,
  51. // passing expected credentials to be used for logging in
  52. const userCredentials = {
  53. email: rawUser.user_email,
  54. name: rawUser.user_name,
  55. seeking: rawUser.is_poster === 1 ? 'poster' : 'seeker',
  56. }
  57. const token = userService.createToken({
  58. payload: userCredentials,
  59. })
  60. return {
  61. ok: true,
  62. handler: pluginConfig.handlerType,
  63. data: {
  64. user_email: user.user_email,
  65. jwt: token,
  66. answered: {
  67. email: userCredentials.email,
  68. name: userCredentials.name,
  69. seeking: userCredentials.seeking,
  70. },
  71. },
  72. }
  73. } catch (err) {
  74. console.error(err)
  75. return {
  76. ok: false,
  77. handler: pluginConfig.handlerType,
  78. data: { error: `${err}` },
  79. }
  80. }
  81. },
  82. validate: validators.post,
  83. response: {
  84. status: {
  85. 201: Joi.object({
  86. ok: Joi.bool(),
  87. handler: Joi.string(),
  88. data: Joi.object({
  89. user_email: Joi.string(),
  90. jwt: Joi.string(),
  91. answered: Joi.object({
  92. email: Joi.string(),
  93. name: Joi.string(),
  94. seeking: Joi.string(),
  95. }),
  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. }