-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignUp.js
73 lines (68 loc) · 2.16 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import bcrypt from 'bcrypt';
import User from "../models/user.js";
const signUp = async (req, res) => {
const body = req.body;
if (!body.firstName) {
res.status(400);
throw new Error('First name of the user is undefined');
}
if (!body.lastName) {
res.status(400);
throw new Error('Last name of the user is undefined');
}
if (!body.email) {
res.status(400);
throw new Error('Email of the user is undefined');
}
if (!body.password) {
res.status(400);
throw new Error('Password of the user is undefined');
}
if (!body.address) {
res.status(400);
throw new Error('Address of the user is undefined');
}
if (!body.address.street) {
res.status(400);
throw new Error('Street of the user is undefined');
}
if (!body.address.street.name) {
res.status(400);
throw new Error('Street name of the user is undefined');
}
if (!body.address.street.houseNumber) {
res.status(400);
throw new Error('House number of the user is undefined');
}
if (!body.address.street.apartmentNumber) {
res.status(400);
throw new Error('House apartment of the user is undefined');
}
if (!body.address.city) {
res.status(400);
throw new Error('City of the user is undefined');
}
if (!body.address.zipCode) {
res.status(400);
throw new Error('Zip code of the user is undefined');
}
const hashedPassword = await bcrypt.hash(body.password, parseInt(process.env.SALT_ROUNDS));
const newUser = await User.create({
firstName: body.firstName,
lastName: body.lastName,
email: body.email,
password: hashedPassword,
address: {
street: {
name: body.address.street.name,
houseNumber: body.address.street.houseNumber,
apartmentNumber: body.address.street.apartmentNumber
},
city: body.address.city,
zipCode: body.address.zipCode
}
});
console.log(newUser);
res.json({ userId: newUser._id.toString() });
};
export { signUp };