| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- 'use strict'
-
- const Joi = require('joi')
- const errorSchema = require('../../schemas/errors')
- const userSchema = require('../../schemas/user')
-
- const pluginConfig = {
- handlerType: 'user',
- docs: {
- description: 'Create a user',
- notes: 'Create a user and other things',
- },
- }
-
- const validators = {
- post: {
- payload: userSchema.userSignup,
- },
- }
-
- const responseSchemas = {
- response: Joi.object({
- user_id: Joi.number(),
- user_name: Joi.string(),
- user_email: Joi.string(),
- is_poster: Joi.number(),
- is_admin: Joi.number(),
- is_verified: Joi.number(),
- }).label('created_user'),
- error: errorSchema.single,
- }
-
- module.exports = {
- method: 'POST',
- path: '/signup',
- options: {
- ...pluginConfig.docs,
- tags: ['api'],
- /** Protect this route with authentication? */
- auth: false,
- cors: true,
- handler: async function (request, h) {
- const { userService } = request.server.services()
- const res = request.payload
- const userName = res.user_name
- const userEmail = res.user_email
- const userType = res.is_poster
- const userPw = res.user_pass ? res.user_pass : 'changeme'
- try {
- const user = await userService.signup({
- password: userPw,
- userInfo: {
- user_name: userName,
- user_email: userEmail,
- is_poster: userType,
- is_admin: 0,
- is_verified: 0,
- },
- })
- return h
- .response({
- ok: true,
- handler: pluginConfig.handlerType,
- data: user,
- })
- .code(201)
- } catch (err) {
- return h
- .response({
- ok: false,
- handler: pluginConfig.handlerType,
- data: { error: `${err}` },
- })
- .code(409)
- }
- },
-
- /** Validate based on validators object */
- validate: {
- ...validators.post,
- failAction: 'log',
- },
-
- /** Validate the server response */
- response: {
- status: {
- 201: Joi.object({
- ok: Joi.bool(),
- handler: Joi.string(),
- data: responseSchemas.response,
- }).label('created_user_res'),
- 409: Joi.object({
- ok: Joi.bool(),
- handler: Joi.string(),
- data: responseSchemas.error,
- }).label('error_single_res'),
- },
- },
- },
- }
|