-
Notifications
You must be signed in to change notification settings - Fork 33
/
signup.js
42 lines (34 loc) · 1.23 KB
/
signup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict';
const Joi = require('joi');
const Helpers = require('../helpers');
const User = require('../../models/user');
module.exports = Helpers.withDefaults({
method: 'post',
path: '/users',
options: {
validate: {
payload: Joi.object({
user: Joi.object().required().keys({
email: User.field('email').required(),
password: Joi.string().required(),
username: User.field('username').required(),
bio: User.field('bio'),
image: User.field('image')
})
})
},
handler: async (request, h) => {
const { user: userInfo } = request.payload;
const { userService, displayService } = request.services();
const signupAndFetchUser = async (txn) => {
const id = await userService.signup(userInfo, txn);
return await userService.findById(id, txn);
};
const user = await h.context.transaction(signupAndFetchUser);
const token = userService.createToken(user.id);
return {
user: displayService.user(user, token)
};
}
}
});