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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. }
  17. /** Validator functions by request method */
  18. const validators = {
  19. post: {
  20. payload: Joi.object({
  21. user_email: Joi.string(),
  22. password: Joi.string(),
  23. }),
  24. },
  25. user: userSchema.single,
  26. error: errorSchema.single,
  27. }
  28. module.exports = {
  29. method: 'POST',
  30. path: '/login',
  31. options: {
  32. ...pluginConfig.docs,
  33. ...pluginConfig.opts,
  34. handler: async function (request, h) {
  35. try {
  36. const { userService } = request.services()
  37. const res = request.payload
  38. // Callback to use as transaction
  39. const login = async txn => {
  40. return await userService.login(
  41. {
  42. email: res.user_email,
  43. password: res.password,
  44. },
  45. txn,
  46. )
  47. }
  48. // Bound context from your plugin server declaration
  49. const user = await h.context.transaction(login)
  50. const token = userService.createToken(user)
  51. return {
  52. ok: true,
  53. handler: pluginConfig.handlerType,
  54. data: { user_email: user.user_email, jwtToken: token },
  55. }
  56. } catch (err) {
  57. console.error(err)
  58. return {
  59. ok: false,
  60. handler: pluginConfig.handlerType,
  61. data: { error: `${err}` },
  62. }
  63. }
  64. },
  65. validate: validators.post,
  66. response: {
  67. status: {
  68. 201: Joi.object({
  69. ok: Joi.bool(),
  70. handler: Joi.string(),
  71. data: Joi.object({
  72. user_email: Joi.string(),
  73. jwtToken: Joi.string(),
  74. }),
  75. }).label('login_res'),
  76. 409: Joi.object({
  77. ok: Joi.bool(),
  78. handler: Joi.string(),
  79. data: validators.error,
  80. }).label('login_error'),
  81. },
  82. },
  83. },
  84. }