您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

signup.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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: userSchema.userSignup,
  15. },
  16. }
  17. const responseSchemas = {
  18. response: Joi.object({
  19. user_id: Joi.number(),
  20. user_name: Joi.string(),
  21. user_email: Joi.string(),
  22. is_poster: Joi.number(),
  23. is_admin: Joi.number(),
  24. is_verified: Joi.number(),
  25. }).label('created_user'),
  26. error: errorSchema.single,
  27. }
  28. // Brevo logic will go here,
  29. // send Brevo email with link that has jwt in it?
  30. module.exports = {
  31. method: 'POST',
  32. path: '/signup',
  33. options: {
  34. ...pluginConfig.docs,
  35. tags: ['api'],
  36. /** Protect this route with authentication? */
  37. auth: false,
  38. cors: true,
  39. handler: async function (request, h) {
  40. const { userService } = request.server.services()
  41. const res = request.payload
  42. const userName = res.user_name
  43. const userEmail = res.user_email
  44. const userType = res.is_poster
  45. const userPw = res.user_pass ? res.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. created_at: Date.now(),
  57. })
  58. return h
  59. .response({
  60. ok: true,
  61. handler: pluginConfig.handlerType,
  62. data: user,
  63. })
  64. .code(201)
  65. } catch (err) {
  66. console.error('ERROR :=>', err)
  67. return h
  68. .response({
  69. ok: false,
  70. handler: pluginConfig.handlerType,
  71. data: { error: `${err}` },
  72. })
  73. .code(409)
  74. }
  75. },
  76. /** Validate based on validators object */
  77. validate: {
  78. ...validators.post,
  79. failAction: 'log',
  80. },
  81. /** Validate the server response */
  82. response: {
  83. status: {
  84. 201: Joi.object({
  85. ok: Joi.bool(),
  86. handler: Joi.string(),
  87. data: responseSchemas.response,
  88. }).label('created_user_res'),
  89. 409: Joi.object({
  90. ok: Joi.bool(),
  91. handler: Joi.string(),
  92. data: responseSchemas.error,
  93. }).label('error_single_res'),
  94. },
  95. },
  96. },
  97. }