You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

signup.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. created_at: Date.now()
  55. })
  56. return h
  57. .response({
  58. ok: true,
  59. handler: pluginConfig.handlerType,
  60. data: user,
  61. })
  62. .code(201)
  63. } catch (err) {
  64. return h
  65. .response({
  66. ok: false,
  67. handler: pluginConfig.handlerType,
  68. data: { error: `${err}` },
  69. })
  70. .code(409)
  71. }
  72. },
  73. /** Validate based on validators object */
  74. validate: {
  75. ...validators.post,
  76. failAction: 'log',
  77. },
  78. /** Validate the server response */
  79. response: {
  80. status: {
  81. 201: Joi.object({
  82. ok: Joi.bool(),
  83. handler: Joi.string(),
  84. data: responseSchemas.response,
  85. }).label('created_user_res'),
  86. 409: Joi.object({
  87. ok: Joi.bool(),
  88. handler: Joi.string(),
  89. data: responseSchemas.error,
  90. }).label('error_single_res'),
  91. },
  92. },
  93. },
  94. }