Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

signup.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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: 'Create a user',
  9. notes: 'Create a user and other things',
  10. },
  11. }
  12. const validators = {
  13. post: {
  14. payload: Joi.object({
  15. user: userSchema.userSignup,
  16. error: errorSchema.single,
  17. }).append().label('signup_payload')
  18. }
  19. }
  20. const responseSchemas = {
  21. response: Joi.object({
  22. user_id: Joi.number(),
  23. user_name: Joi.string(),
  24. user_email: Joi.string(),
  25. is_poster: Joi.number(),
  26. is_admin: Joi.number(),
  27. is_verified: Joi.number(),
  28. }).label('created_user'),
  29. error: errorSchema.single
  30. }
  31. module.exports = {
  32. method: 'POST',
  33. path: '/signup',
  34. options: {
  35. ...pluginConfig.docs,
  36. tags: ['api'],
  37. /** Protect this route with authentication? */
  38. auth: false,
  39. handler: async function (request, h) {
  40. const { userService } = request.server.services()
  41. const res = request.payload
  42. const userName = res.user.user_name
  43. const userEmail = res.user.user_email
  44. const userType = res.user.user_type == 'poster' ? 1 : 0
  45. const userPw = res.user.user_pass ? res.user.user_pass : 'changeme'
  46. try {
  47. const user = await userService.signup({
  48. password: userPw,
  49. userInfo: {
  50. user_name: userName,
  51. user_email: userEmail,
  52. is_poster: userType,
  53. is_admin: 0,
  54. is_verified: 0,
  55. }
  56. })
  57. return h
  58. .response({
  59. ok: true,
  60. handler: pluginConfig.handlerType,
  61. data: user,
  62. })
  63. .code(201)
  64. } catch (err) {
  65. return h
  66. .response({
  67. ok: false,
  68. handler: pluginConfig.handlerType,
  69. data: { error: `${err}` },
  70. })
  71. .code(409)
  72. }
  73. },
  74. /** Validate based on validators object */
  75. validate: {
  76. ...validators.post,
  77. failAction: 'log',
  78. },
  79. /** Validate the server response */
  80. response: {
  81. status: {
  82. 201: Joi.object({
  83. ok: Joi.bool(),
  84. handler: Joi.string(),
  85. data: responseSchemas.response,
  86. }).label('created_user_res'),
  87. 409: Joi.object({
  88. ok: Joi.bool(),
  89. handler: Joi.string(),
  90. data: responseSchemas.error,
  91. }).label('error_single_res'),
  92. },
  93. },
  94. },
  95. }