Repository navigation
Expand file tree
/
Copy pathusers.ts
More file actions
153 lines (146 loc) · 4.69 KB
/
Copy pathusers.ts
File metadata and controls
153 lines (146 loc) · 4.69 KB
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
import express from 'express';
import _ from 'lodash';
import { AppError, AppErrorI18n } from '../core/app-error';
import { checkToken, Req } from '../core/middleware';
import { accountManager } from '../core/services/account-manager';
import { Users } from '../models/users';
export const usersRouter = express.Router();
usersRouter.get('/', checkToken, (req: Req, res) => {
res.send({ title: 'CodePushServer' });
});
usersRouter.post(
'/',
(
req: Req<
void,
{
email: string;
token: string;
password: string;
},
void
>,
res,
next,
) => {
const { logger, body } = req;
const email = _.trim(body.email);
const token = _.trim(body.token);
const password = _.trim(body.password);
logger.info('try register account', { email, token });
return accountManager
.checkRegisterCode(email, token)
.then(() => {
if (_.isString(password) && password.length < 6) {
throw new AppErrorI18n('error.password_length_register');
}
return accountManager.register(email, password);
})
.then(() => {
logger.info('register account success', { email });
res.send({ status: 'OK' });
})
.catch((e) => {
if (e instanceof AppError) {
logger.info('register account failed', { email, error: e.message });
res.send({ status: 'ERROR', message: e.message });
} else {
next(e);
}
});
},
);
usersRouter.get('/exists', (req: Req<void, void, { email: string }>, res, next) => {
const email = _.trim(req.query.email);
const emailRequiredMessage = req.t('error.input_email_required');
if (!email) {
res.send({ status: 'ERROR', message: emailRequiredMessage });
return;
}
Users.findOne({ where: { email } })
.then((u) => {
res.send({ status: 'OK', exists: !!u });
})
.catch((e) => {
if (e instanceof AppError) {
res.send({ status: 'ERROR', message: e.message });
} else {
next(e);
}
});
});
usersRouter.post('/registerCode', (req: Req<void, { email: string }, void>, res, next) => {
const { logger, body } = req;
const { email } = body;
logger.info('try send register code', { email });
return accountManager
.sendRegisterCode(email)
.then(() => {
logger.info('send register code success', { email });
res.send({ status: 'OK' });
})
.catch((e) => {
if (e instanceof AppError) {
logger.info('send register code error', { email, error: e.message });
res.send({ status: 'ERROR', message: e.message });
} else {
next(e);
}
});
});
usersRouter.get(
'/registerCode/exists',
(req: Req<void, void, { email: string; token: string }>, res, next) => {
const { query } = req;
const email = _.trim(query.email);
const token = _.trim(query.token);
return accountManager
.checkRegisterCode(email, token)
.then(() => {
res.send({ status: 'OK' });
})
.catch((e) => {
if (e instanceof AppError) {
res.send({ status: 'ERROR', message: e.message });
} else {
next(e);
}
});
},
);
// Change Password
usersRouter.patch(
'/password',
checkToken,
(
req: Req<
Record<string, string>,
{
oldPassword: string;
newPassword: string;
}
>,
res,
next,
) => {
const { logger, body } = req;
const oldPassword = _.trim(body.oldPassword);
const newPassword = _.trim(body.newPassword);
const uid = req.users.id;
logger.info('try change password', { uid });
return accountManager
.changePassword(uid, oldPassword, newPassword)
.then(() => {
logger.info('change password success', { uid });
res.send({ status: 'OK' });
})
.catch((e) => {
if (e instanceof AppError) {
logger.info('change password failed', { uid });
res.send({ status: 'ERROR', message: e.message });
} else {
next(e);
}
});
},
);