選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

signup.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. module.exports = {
  29. method: 'POST',
  30. path: '/signup',
  31. options: {
  32. ...pluginConfig.docs,
  33. tags: ['api'],
  34. /** Protect this route with authentication? */
  35. auth: false,
  36. cors: true,
  37. handler: async function (request, h) {
  38. const { userService } = request.server.services()
  39. const res = request.payload
  40. const userName = res.user_name
  41. const userEmail = res.user_email
  42. const userType = res.is_poster
  43. const userPw = res.user_pass ? res.user_pass : 'changeme'
  44. try {
  45. const user = await userService.signup({
  46. password: userPw,
  47. userInfo: {
  48. user_name: userName,
  49. user_email: userEmail,
  50. is_poster: userType,
  51. is_admin: 0,
  52. is_verified: 0,
  53. },
  54. })
  55. return h
  56. .response({
  57. ok: true,
  58. handler: pluginConfig.handlerType,
  59. data: user,
  60. })
  61. .code(201)
  62. } catch (err) {
  63. return h
  64. .response({
  65. ok: false,
  66. handler: pluginConfig.handlerType,
  67. data: { error: `${err}` },
  68. })
  69. .code(409)
  70. }
  71. },
  72. /** Validate based on validators object */
  73. validate: {
  74. ...validators.post,
  75. failAction: 'log',
  76. },
  77. /** Validate the server response */
  78. response: {
  79. status: {
  80. 201: Joi.object({
  81. ok: Joi.bool(),
  82. handler: Joi.string(),
  83. data: responseSchemas.response,
  84. }).label('created_user_res'),
  85. 409: Joi.object({
  86. ok: Joi.bool(),
  87. handler: Joi.string(),
  88. data: responseSchemas.error,
  89. }).label('error_single_res'),
  90. },
  91. },
  92. },
  93. }