/
crud.js
314 lines (279 loc) · 9.25 KB
/
crud.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
* @apiDefine AccountExistError
*
* @apiError AccountExist Account with the same name already exist.
*
* @apiErrorExample Error-Response:
* HTTP/1.1 500 Server error
* {
* "error": "Account name already registered"
* }
*/
/**
* @apiDefine AccountNotFoundError
*
* @apiError AccountNotFound The account with given name doest not found.
*
* @apiErrorExample Error-Response:
* HTTP/1.1 404 Not Found
* {
* "error": "AccountNotFound"
* }
*/
'use strict';
var _ = require('lodash');
var express = require('express');
var CryptoJS = require('crypto-js');
var jwt = require('jsonwebtoken');
var router = express.Router();
var validator = require('validator');
var passport = require('passport');
var config = require('../config.js');
var firebaseClient = require('../firebase-client/index');
var USER_PATH = '/account';
var PROFILE_FIELDS = [
'first_name',
'middle_name',
'last_name',
'date_of_birth',
'email_address',
'street_number',
'street_name',
'unit_number',
'floor_number',
'city',
'county',
'district',
'state',
'region',
'province',
'territory',
'postal_code',
'country',
'user_home_latlng',
'telephone_country_code',
'telephone_area_code',
'telephone_number',
'occupation',
'industry'
];
var getAccountByAccountName = function (accountName) {
return firebaseClient().then(function (instance) {
return instance.get(USER_PATH + '/' + accountName);
});
};
router.post('/auth', function (req, res, next) {
getAccountByAccountName(req.body.username)
.then(function (response) {
var data = response.data;
if (data) {
var payload = data;
if (String(CryptoJS.MD5(req.body.password)) == payload.password) {
delete payload.password;
var token = jwt.sign({ username: payload.account }, config.get('API_SECRET'), {
expiresIn: config.get('TOKEN_EXPIRE_TIME')
});
res.status(200).json({
user: payload,
token: 'JWT ' + token
});
} else {
res.status(400).json({ error: 'Incorrect password' });
}
} else {
res.status(400).json({ error: 'User not found' });
}
}).catch(function (err) {
res.status(500).json(err);
});
});
router.get('/auth', passport.authenticate('jwt', { session: false }), function (req, res) {
getAccountByAccountName(req.user.username).then(function (response) {
delete response.data.password;
res.status(200).json({
user: response.data
});
}).catch(function (err) {
res.status(500).json(err);
});
});
/**
* @api {post} /account Create a new account
* @apiVersion 0.0.1
* @apiName PostAccount
* @apiGroup Account
* @apiPermission none
*
* @apiDescription Create account.
*
* @apiParam {String} account Account Name - Required.
* @apiParam {String} Password Passowrd of account - Required.
* @apiParam {String} first_name First name of account holder.
* @apiParam {String} middle_name Moddile name of account holder.
* @apiParam {String} last_name Last name of account holder.
* @apiParam {String} date_of_birth Birthdate of account holder.
* @apiParam {String} email_address Last name of account holder.
* @apiParam {String} street_number Street numner of address.
* @apiParam {String} street_name Street name of address.
* @apiParam {String} unit_number Unit number of address.
* @apiParam {String} floor_number Floor number of address.
* @apiParam {String} city City of account holder.
* @apiParam {String} county Country of account holder.
* @apiParam {String} district District of account holder.
* @apiParam {String} state State of account holder.
* @apiParam {String} region Region of account holder.
* @apiParam {String} province Address province of address.
* @apiParam {String} territory Address territory of address.
* @apiParam {String} postal_code Address postal code of address.
* @apiParam {String} user_home_latlng Latitude and Longitude of account holder's home.
* @apiParam {String} telephone_country_code Country code of account holder.
* @apiParam {String} telephone_area_code Area code of account holder.
* @apiParam {String} telephone_number Telephone numner of account holder.
* @apiParam {String} occupation Occupation of account holder.
* @apiParam {String} industry Work industry of account holder.
*
* @apiSuccess {Object} data The new account's object.
*
* @apiUse AccountExistError
*/
var putAccount = function (auth) {
return firebaseClient().then(function (instance) {
return instance.put(USER_PATH + '/' + auth.account, auth);
});
};
router.post('/', function (req, res) {
var body = req.body;
if (!body.account || !body.password) {
res.status(400).json({ error: 'account name and password cannot be empty' });
return;
}
var auth = ['account', 'password'];
var authParams = _.pick(body, auth);
var profileParams = _.pick(body, PROFILE_FIELDS);
profileParams.created_time = new Date();
authParams.password = String(CryptoJS.MD5(authParams.password));
var account_profile = _.isEmpty(profileParams) ? null : { account_profile: [profileParams] };
getAccountByAccountName(body.account)
.then(function (response) {
if (response.data) {
res.status(400).json({ error: 'Account name already registered' });
} else {
putAccount(_.assign(authParams, account_profile)).then(function (response) {
res.status(200).json({ account: response.data.account });
}).catch(function (err) {
res.status(500).json(err);
});
}
}).catch(function (err) {
res.status(500).json(err);
});
});
var patchEmail = function (account, email) {
return firebaseClient().then(function (instance) {
return instance.patch([USER_PATH, account, 'account_profile', 0].join('/'), { email_address: email });
});
};
router.patch('/email', passport.authenticate('jwt', { session: false }), function (req, res) {
var body = req.body;
if (!body.account) {
res.status(500).json({ error: 'Account is undefined' });
return;
}
if (!body.email) {
res.status(500).json({ error: 'Email required' });
return;
}
if (!validator.isEmail(body.email)) {
res.status(500).json({ error: 'Invalid email' });
return;
}
patchEmail(body.account, body.email).then(function (response) {
res.status(200).json({ success: true });
}).catch(function (err) {
res.status(500).json(err);
});
});
var patchPassword = function (account, password) {
return firebaseClient().then(function (instance) {
return instance.patch([USER_PATH, account].join('/'), { password: String(CryptoJS.MD5(password)) });
});
};
router.patch('/password', passport.authenticate('jwt', { session: false }), function (req, res) {
var body = req.body;
if (!body.account) {
res.status(500).json({ error: 'Account is undefined' });
return;
}
if (!body.new_password || !body.old_password) {
res.status(500).json({ error: 'Password required' });
return;
}
if (body.new_password.length < 8
|| /\s/.test(body.new_password)
|| !/([A-Z]|[a-z])+[0-9]+[~@#$^*()_+=[\]{}|\\,.?:-]*/g.test(body.new_password)) {
res.status(500).json({
error: [
'Password must be 8 characters,',
'both numbers and letters,',
'special characters permitted,',
'spaces not permitted'].join(' ')
});
return;
}
getAccountByAccountName(body.account)
.then(function (response) {
if (response.data && response.data.password === String(CryptoJS.MD5(body.old_password))) {
patchPassword(body.account, body.new_password).then(function (response) {
res.status(200).json({ success: true });
}).catch(function (err) {
res.status(500).json(err);
});
} else {
res.status(400).json({ error: 'Old password incorrect' });
}
}).catch(function (err) {
res.status(500).json(err);
});
});
var putProfile = function (account, profile) {
return firebaseClient().then(function (instance) {
return instance.put([USER_PATH, account, 'account_profile'].join('/'), profile);
});
};
router.patch('/profile', passport.authenticate('jwt', { session: false }), function (req, res) {
var account = req.body.account;
var password = req.body.password;
var profile = req.body.profile;
if (!account) {
res.status(500).json({ error: 'Account is undefined' });
return;
}
if (!password) {
res.status(500).json({ error: 'Password required' });
return;
}
profile = _.pick(profile, PROFILE_FIELDS);
profile.created_time = new Date();
getAccountByAccountName(account).then(function (response) {
if (response.data.password === String(CryptoJS.MD5(password))) {
if (response.data.account_profile) {
profile.email_address = response.data.account_profile[0].email_address;
profile = [profile].concat(response.data.account_profile);
} else {
profile = [profile];
}
return putProfile(account, profile);
} else {
res.status(400).json({ error: 'Password incorrect' });
}
}).then(function (response) {
res.status(200).json({ success: true });
}).catch(function (err) {
res.status(500).json(err);
});
});
router.use(function handleRpcError(err, req, res, next) {
err.response = err.message;
next(err);
});
module.exports = router;