-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.js
173 lines (156 loc) · 5 KB
/
register.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const bcrypt = require('bcrypt');
const Handlebars = require('handlebars');
const fs = require('fs');
const uuid = require('uuid');
const _ = require('lodash');
const validateParams = require('./validate_params');
const ERR_INVALID_PARAMS = 'Invalid Parameters';
const ERR_ORGANIZATION_NOT_UNIQUE = 'Organization is not unique';
const ERR_USER_NOT_UNIQUE = 'User is not unique';
const ERR_PASSWORD_GENERATION = 'Password generation error';
const ERR_EMAIL_NOTIFICATION = 'Email notification error';
module.exports = (r, email, settings) => {
const checkUniqueOrganization = (params) => {
if (settings.multitenancy) {
return r
.table('organizations')
.getAll(params.organization.name, { index: 'name' })
.then((result) => {
if (Array.isArray(result) && result.length > 0) {
throw new Error(ERR_ORGANIZATION_NOT_UNIQUE);
} else {
return params;
}
});
} else {
return new Promise((resolve) => resolve(params));
}
};
const checkUniqueUser = (params) => {
return r
.table('users')
.getAll(params.user.email, {index: 'email'})
.then((result) => {
if (Array.isArray(result) && result.length > 0) {
throw new Error(ERR_USER_NOT_UNIQUE);
} else {
return params;
}
});
};
const createOrganization = (params) => {
if (settings.multitenancy) {
return r
.table('organizations')
.insert(params.organization)
.then((result) => {
params.organization.id = result.generated_keys[0];
return params;
});
} else {
return new Promise((resolve) => resolve(params));
}
};
const createUser = (params) => {
// add perishable token for email confirmation
params.user.perishableToken = uuid.v4();
params.user.emailConfirmed = false;
params.user.role = 'superuser';
if (settings.multitenancy) {
params.user.organization_id = params.organization.id;
}
return r
.table('users')
.insert(params.user)
.then((result) => {
params.user.id = result.generated_keys[0];
return params;
});
};
const encryptPassword = (params) => {
return new Promise((resolve, reject) => {
bcrypt.hash(params.user.password, 13, (err, hash) => {
if (err) {
reject(new Error(ERR_PASSWORD_GENERATION));
} else {
delete params.user.passwordConfirmation;
delete params.user.perishableToken;
params.user.password = hash;
resolve(params);
}
});
});
};
const notify = (req) => {
return (params) => {
return new Promise((resolve, reject) => {
const data = {
params: params,
site: {
name: settings.site.name
},
activation_link: `${settings.site.url}${req.baseUrl}/activate?token=${params.user.perishableToken}&redirect_to=${settings.site.url}`
};
const html = settings.emails.confirm.template(data);
const message = {
text: "",
from: settings.email.from,
subject: settings.emails.confirm.subject,
to: params.user.email,
attachment: [
{ data: html, alternative: true }
]
};
email.send(message, (error, message) => {
if (error) {
reject(new Error(ERR_EMAIL_NOTIFICATION));
} else {
resolve(params);
}
});
});
};
};
const validateRegistration = (params) => {
const organizationValid = settings.multitenancy ? validateParams(params.organization, ['name']) : true;
return organizationValid &&
validateParams(params.user, ['firstName', 'lastName', 'email', 'password', 'passwordConfirmation']) &&
params.user.password == params.user.passwordConfirmation;
};
this.register = (req, res) => {
const required = ['user'];
if (settings.multitenancy) {
required.push('organization');
}
if (validateParams(req.body, required)) {
const params = req.body;
params.user.createdAt = new Date();
params.user.updatedAt = new Date();
if (settings.multitenancy) {
params.organization.createdAt = new Date();
params.organization.updatedAt = new Date();
}
if (validateRegistration(params)) {
checkUniqueOrganization(params)
.then(checkUniqueUser)
.then(encryptPassword)
.then(createOrganization)
.then(createUser)
.then(notify(req))
.then((params) => {
delete params.user.password;
delete params.user.perishableToken;
res.send(params);
})
.catch((error) => {
res.status(500).send(error.message);
});
} else {
res.status(500).send(ERR_INVALID_PARAMS);
}
} else {
res.status(500).send(ERR_INVALID_PARAMS);
}
};
return this.register;
};