underpost 2.6.3 → 2.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dockerignore +13 -13
- package/.env.development +7 -7
- package/.env.production +7 -7
- package/.env.test +7 -7
- package/.github/workflows/publish.yml +26 -0
- package/.nycrc +9 -9
- package/.prettierignore +12 -12
- package/.prettierrc +9 -9
- package/.vscode/extensions.json +72 -72
- package/.vscode/settings.json +100 -99
- package/AUTHORS.md +10 -0
- package/CHANGELOG.md +91 -0
- package/Dockerfile +89 -89
- package/LICENSE +21 -21
- package/README.md +96 -96
- package/bin/db.js +172 -119
- package/bin/deploy.js +582 -626
- package/bin/dns.js +1 -1
- package/bin/file.js +92 -92
- package/bin/index.js +53 -34
- package/bin/install.js +398 -357
- package/bin/shortcut.js +44 -44
- package/bin/ssl.js +65 -61
- package/bin/util.js +182 -182
- package/bin/vs.js +35 -35
- package/conf.js +251 -249
- package/docker-compose.yml +67 -67
- package/jsconfig.json +7 -7
- package/jsdoc.json +32 -32
- package/nodemon.json +6 -6
- package/package.json +137 -128
- package/prometheus.yml +36 -36
- package/setup.sh +24 -24
- package/src/api/core/core.controller.js +69 -69
- package/src/api/core/core.model.js +11 -11
- package/src/api/core/core.router.js +23 -23
- package/src/api/core/core.service.js +29 -29
- package/src/api/crypto/crypto.controller.js +51 -51
- package/src/api/crypto/crypto.model.js +23 -23
- package/src/api/crypto/crypto.router.js +20 -20
- package/src/api/crypto/crypto.service.js +64 -64
- package/src/api/default/default.controller.js +69 -69
- package/src/api/default/default.model.js +20 -20
- package/src/api/default/default.router.js +23 -23
- package/src/api/default/default.service.js +31 -31
- package/src/api/file/file.controller.js +53 -51
- package/src/api/file/file.model.js +19 -19
- package/src/api/file/file.router.js +21 -20
- package/src/api/file/file.service.js +76 -70
- package/src/api/instance/instance.controller.js +69 -69
- package/src/api/instance/instance.model.js +36 -36
- package/src/api/instance/instance.router.js +33 -33
- package/src/api/instance/instance.service.js +48 -48
- package/src/api/test/test.controller.js +59 -59
- package/src/api/test/test.model.js +14 -14
- package/src/api/test/test.router.js +21 -21
- package/src/api/test/test.service.js +35 -35
- package/src/api/user/user.build.js +16 -0
- package/src/api/user/user.controller.js +70 -70
- package/src/api/user/user.model.js +65 -65
- package/src/api/user/user.router.js +345 -345
- package/src/api/user/user.service.js +479 -479
- package/src/api.js +23 -23
- package/src/client/Default.index.js +40 -40
- package/src/client/components/core/Account.js +290 -290
- package/src/client/components/core/AgGrid.js +160 -160
- package/src/client/components/core/Auth.js +19 -19
- package/src/client/components/core/Badge.js +32 -32
- package/src/client/components/core/BlockChain.js +41 -41
- package/src/client/components/core/Blog.js +9 -9
- package/src/client/components/core/BtnIcon.js +101 -94
- package/src/client/components/core/CalendarCore.js +458 -319
- package/src/client/components/core/Chat.js +64 -64
- package/src/client/components/core/ColorPalette.js +5267 -5267
- package/src/client/components/core/CommonJs.js +735 -732
- package/src/client/components/core/Content.js +193 -49
- package/src/client/components/core/Css.js +1064 -1027
- package/src/client/components/core/CssCore.js +817 -796
- package/src/client/components/core/D3Chart.js +44 -44
- package/src/client/components/core/Docs.js +229 -229
- package/src/client/components/core/DropDown.js +164 -164
- package/src/client/components/core/EventsUI.js +46 -54
- package/src/client/components/core/FileExplorer.js +699 -624
- package/src/client/components/core/FullScreen.js +45 -45
- package/src/client/components/core/Input.js +346 -259
- package/src/client/components/core/JoyStick.js +77 -77
- package/src/client/components/core/Keyboard.js +73 -73
- package/src/client/components/core/LoadingAnimation.js +179 -157
- package/src/client/components/core/LogIn.js +187 -181
- package/src/client/components/core/LogOut.js +58 -52
- package/src/client/components/core/Logger.js +26 -26
- package/src/client/components/core/Modal.js +1612 -1596
- package/src/client/components/core/NotificationManager.js +84 -84
- package/src/client/components/core/Panel.js +613 -413
- package/src/client/components/core/PanelForm.js +468 -0
- package/src/client/components/core/Polyhedron.js +162 -162
- package/src/client/components/core/Recover.js +204 -204
- package/src/client/components/core/Responsive.js +53 -53
- package/src/client/components/core/RichText.js +51 -27
- package/src/client/components/core/Router.js +76 -77
- package/src/client/components/core/Scroll.js +34 -0
- package/src/client/components/core/SignUp.js +125 -125
- package/src/client/components/core/SocketIo.js +72 -72
- package/src/client/components/core/Stream.js +113 -113
- package/src/client/components/core/ToggleSwitch.js +87 -87
- package/src/client/components/core/ToolTip.js +26 -26
- package/src/client/components/core/Translate.js +437 -408
- package/src/client/components/core/Validator.js +100 -100
- package/src/client/components/core/VanillaJs.js +460 -457
- package/src/client/components/core/Wallet.js +106 -106
- package/src/client/components/core/Webhook.js +25 -25
- package/src/client/components/core/Worker.js +272 -272
- package/src/client/components/default/CommonDefault.js +29 -29
- package/src/client/components/default/CssDefault.js +13 -13
- package/src/client/components/default/ElementsDefault.js +38 -38
- package/src/client/components/default/LogInDefault.js +41 -41
- package/src/client/components/default/LogOutDefault.js +28 -28
- package/src/client/components/default/MenuDefault.js +389 -389
- package/src/client/components/default/RoutesDefault.js +48 -48
- package/src/client/components/default/SettingsDefault.js +16 -16
- package/src/client/components/default/SignUpDefault.js +9 -9
- package/src/client/components/default/SocketIoDefault.js +54 -54
- package/src/client/components/default/TranslateDefault.js +7 -7
- package/src/client/public/default/assets/mailer/api-user-check.png +0 -0
- package/src/client/public/default/assets/mailer/api-user-invalid-token.png +0 -0
- package/src/client/public/default/assets/mailer/api-user-recover.png +0 -0
- package/src/client/public/default/browserconfig.xml +11 -11
- package/src/client/public/default/manifest.webmanifest +68 -68
- package/src/client/public/default/plantuml/client-conf.svg +1 -0
- package/src/client/public/default/plantuml/client-schema.svg +1 -0
- package/src/client/public/default/plantuml/cron-conf.svg +1 -0
- package/src/client/public/default/plantuml/cron-schema.svg +1 -0
- package/src/client/public/default/plantuml/server-conf.svg +1 -0
- package/src/client/public/default/plantuml/server-schema.svg +1 -0
- package/src/client/public/default/plantuml/ssr-conf.svg +1 -0
- package/src/client/public/default/plantuml/ssr-schema.svg +1 -0
- package/src/client/public/default/sitemap +147 -147
- package/src/client/public/default/yandex-browser-manifest.json +8 -8
- package/src/client/public/doc/sitemap +147 -147
- package/src/client/public/test/sitemap +147 -147
- package/src/client/services/core/core.service.js +170 -152
- package/src/client/services/crypto/crypto.service.js +70 -70
- package/src/client/services/default/default.management.js +345 -345
- package/src/client/services/default/default.service.js +89 -89
- package/src/client/services/file/file.service.js +70 -70
- package/src/client/services/instance/instance.management.js +74 -74
- package/src/client/services/instance/instance.service.js +89 -89
- package/src/client/services/test/test.service.js +70 -70
- package/src/client/services/user/user.management.js +50 -50
- package/src/client/services/user/user.service.js +89 -89
- package/src/client/ssr/Render.js +16 -16
- package/src/client/ssr/body-components/CacheControl.js +114 -113
- package/src/client/ssr/body-components/DefaultSplashScreen.js +79 -79
- package/src/client/ssr/email-components/DefaultRecoverEmail.js +21 -21
- package/src/client/ssr/email-components/DefaultVerifyEmail.js +17 -17
- package/src/client/ssr/head-components/Css.js +241 -241
- package/src/client/ssr/head-components/DefaultScripts.js +3 -3
- package/src/client/ssr/head-components/Microdata.js +11 -11
- package/src/client/ssr/head-components/Production.js +1 -1
- package/src/client/ssr/head-components/PwaDefault.js +59 -59
- package/src/client/ssr/head-components/Seo.js +14 -14
- package/src/client/sw/default.sw.js +201 -201
- package/src/client/sw/template.sw.js +84 -84
- package/src/client.build.js +22 -22
- package/src/client.dev.js +21 -21
- package/src/cron.js +25 -25
- package/src/db/DataBaseProvider.js +34 -34
- package/src/db/mariadb/MariaDB.js +33 -33
- package/src/db/mongo/MongooseDB.js +46 -46
- package/src/dns.js +22 -22
- package/src/index.js +42 -0
- package/src/mailer/EmailRender.js +69 -69
- package/src/mailer/MailerProvider.js +96 -96
- package/src/proxy.js +22 -22
- package/src/runtime/lampp/Lampp.js +69 -44
- package/src/runtime/nginx/Nginx.js +3 -3
- package/src/runtime/xampp/Xampp.js +49 -49
- package/src/server/auth.js +235 -204
- package/src/server/backup.js +101 -84
- package/src/server/client-build-live.js +72 -72
- package/src/server/client-build.js +705 -699
- package/src/server/client-dev-server.js +60 -58
- package/src/server/client-formatted.js +48 -48
- package/src/server/client-icons.js +149 -150
- package/src/server/conf.js +860 -611
- package/src/server/dns.js +98 -87
- package/src/server/downloader.js +42 -42
- package/src/server/logger.js +180 -135
- package/src/server/network.js +122 -122
- package/src/server/peer.js +33 -33
- package/src/server/process.js +66 -66
- package/src/server/prompt-optimizer.js +28 -0
- package/src/server/proxy.js +118 -118
- package/src/server/runtime.js +444 -393
- package/src/server/ssl.js +109 -107
- package/src/server.js +25 -25
- package/src/ws/IoInterface.js +45 -45
- package/src/ws/IoServer.js +39 -39
- package/src/ws/core/channels/core.ws.chat.js +23 -23
- package/src/ws/core/channels/core.ws.mailer.js +35 -35
- package/src/ws/core/channels/core.ws.stream.js +31 -31
- package/src/ws/core/core.ws.connection.js +28 -28
- package/src/ws/core/core.ws.emit.js +14 -14
- package/src/ws/core/core.ws.server.js +24 -24
- package/src/ws/core/management/core.ws.chat.js +8 -8
- package/src/ws/core/management/core.ws.mailer.js +16 -16
- package/src/ws/core/management/core.ws.stream.js +8 -8
- package/src/ws/default/channels/default.ws.main.js +16 -16
- package/src/ws/default/default.ws.connection.js +22 -22
- package/src/ws/default/default.ws.emit.js +14 -14
- package/src/ws/default/default.ws.server.js +20 -20
- package/src/ws/default/management/default.ws.main.js +8 -8
- package/startup.js +11 -11
- package/supervisord-openssh-server.conf +4 -4
- package/test/api.test.js +60 -60
- package/bin/help.js +0 -110
|
@@ -1,479 +1,479 @@
|
|
|
1
|
-
import { loggerFactory } from '../../server/logger.js';
|
|
2
|
-
import { hashPassword, verifyPassword, hashJWT, verifyJWT, validatePasswordMiddleware } from '../../server/auth.js';
|
|
3
|
-
import { MailerProvider } from '../../mailer/MailerProvider.js';
|
|
4
|
-
import { CoreWsMailerManagement } from '../../ws/core/management/core.ws.mailer.js';
|
|
5
|
-
import { CoreWsEmit } from '../../ws/core/core.ws.emit.js';
|
|
6
|
-
import { CoreWsMailerChannel } from '../../ws/core/channels/core.ws.mailer.js';
|
|
7
|
-
import validator from 'validator';
|
|
8
|
-
import { DataBaseProvider } from '../../db/DataBaseProvider.js';
|
|
9
|
-
import { s4 } from '../../client/components/core/CommonJs.js';
|
|
10
|
-
import { FileFactory } from '../file/file.service.js';
|
|
11
|
-
import fs from 'fs-extra';
|
|
12
|
-
import { svg, png, png3x } from 'font-awesome-assets';
|
|
13
|
-
import { UserDto } from './user.model.js';
|
|
14
|
-
import Jimp from 'jimp';
|
|
15
|
-
|
|
16
|
-
const logger = loggerFactory(import.meta);
|
|
17
|
-
|
|
18
|
-
const getDefaultProfileImageId = async (File) => {
|
|
19
|
-
const faId = 'user';
|
|
20
|
-
const tmpFilePath = `./tmp/${faId}-${s4() + s4()}.svg`;
|
|
21
|
-
fs.writeFileSync(tmpFilePath, svg(faId, '#f5f5f5d1'), 'utf8');
|
|
22
|
-
const file = await new File(FileFactory.svg(fs.readFileSync(tmpFilePath), `${faId}.svg`)).save();
|
|
23
|
-
fs.removeSync(tmpFilePath);
|
|
24
|
-
return file._id;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
const UserService = {
|
|
28
|
-
post: async (req, res, options) => {
|
|
29
|
-
/** @type {import('./user.model.js').UserModel} */
|
|
30
|
-
const User = DataBaseProvider.instance[`${options.host}${options.path}`].mongoose.User;
|
|
31
|
-
|
|
32
|
-
/** @type {import('../file/file.model.js').FileModel} */
|
|
33
|
-
const File = DataBaseProvider.instance[`${options.host}${options.path}`].mongoose.File;
|
|
34
|
-
|
|
35
|
-
if (req.params.id === 'recover-verify-email') {
|
|
36
|
-
const user = await User.findOne({
|
|
37
|
-
email: req.body.email,
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
if (!user) throw new Error('Email address does not exist');
|
|
41
|
-
|
|
42
|
-
const token = hashJWT({ email: req.body.email }, '15m');
|
|
43
|
-
const payloadToken = hashJWT({ email: req.body.email }, '15m');
|
|
44
|
-
const id = `${options.host}${options.path}`;
|
|
45
|
-
const translate = {
|
|
46
|
-
H1: {
|
|
47
|
-
en: 'Recover your account',
|
|
48
|
-
es: 'Recupera tu cuenta',
|
|
49
|
-
},
|
|
50
|
-
P1: {
|
|
51
|
-
en: 'To recover your account, please click the button below:',
|
|
52
|
-
es: 'Para recuperar tu cuenta, haz click en el botón de abajo:',
|
|
53
|
-
},
|
|
54
|
-
BTN_LABEL: {
|
|
55
|
-
en: 'Recover Password',
|
|
56
|
-
es: 'Recuperar Contraseña',
|
|
57
|
-
},
|
|
58
|
-
};
|
|
59
|
-
const sendResult = await MailerProvider.send({
|
|
60
|
-
id,
|
|
61
|
-
sendOptions: {
|
|
62
|
-
to: req.body.email, // list of receivers
|
|
63
|
-
subject: translate.H1[req.lang], // Subject line
|
|
64
|
-
text: translate.H1[req.lang], // plain text body
|
|
65
|
-
html: MailerProvider.instance[id].templates.userRecoverEmail
|
|
66
|
-
.replace('{{H1}}', translate.H1[req.lang])
|
|
67
|
-
.replace('{{P1}}', translate.P1[req.lang])
|
|
68
|
-
.replace('{{TOKEN}}', token)
|
|
69
|
-
.replace(`{{COMPANY}}`, options.host) // html body
|
|
70
|
-
.replace(
|
|
71
|
-
'{{RECOVER_WEB_URL}}',
|
|
72
|
-
`${process.env === 'development' ? 'http://' : 'https://'}${options.host}${options.path}${
|
|
73
|
-
options.path === '/' ? 'recover' : `/recover`
|
|
74
|
-
}?payload=${payloadToken}`,
|
|
75
|
-
)
|
|
76
|
-
.replace('{{RECOVER_BTN_LABEL}}', translate.BTN_LABEL[req.lang]),
|
|
77
|
-
|
|
78
|
-
attachments: [
|
|
79
|
-
// {
|
|
80
|
-
// filename: 'logo.png',
|
|
81
|
-
// path: `./logo.png`,
|
|
82
|
-
// cid: 'logo', // <img src='cid:logo'>
|
|
83
|
-
// },
|
|
84
|
-
],
|
|
85
|
-
},
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
if (!sendResult) throw new Error('email send error');
|
|
89
|
-
return { message: 'email send successfully' };
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (req.path.startsWith('/mailer') && req.params.id === 'verify-email') {
|
|
93
|
-
if (!validator.isEmail(req.body.email)) throw { message: 'invalid email' };
|
|
94
|
-
|
|
95
|
-
const token = hashJWT({ email: req.body.email });
|
|
96
|
-
const id = `${options.host}${options.path}`;
|
|
97
|
-
const user = await User.findById(req.auth.user._id);
|
|
98
|
-
|
|
99
|
-
if (user.emailConfirmed) throw new Error('email already confirmed');
|
|
100
|
-
|
|
101
|
-
if (user.email !== req.body.email) {
|
|
102
|
-
req.body.emailConfirmed = false;
|
|
103
|
-
|
|
104
|
-
const result = await User.findByIdAndUpdate(
|
|
105
|
-
req.auth.user._id,
|
|
106
|
-
{ emailConfirmed: false, email: req.body.email },
|
|
107
|
-
{
|
|
108
|
-
runValidators: true,
|
|
109
|
-
},
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
const translate = {
|
|
113
|
-
H1: {
|
|
114
|
-
en: 'Confirm Your Email',
|
|
115
|
-
zh: '请确认您的电子邮箱',
|
|
116
|
-
es: 'Confirma tu correo electrónico',
|
|
117
|
-
},
|
|
118
|
-
P1: {
|
|
119
|
-
en: 'Email confirmed! Thanks.',
|
|
120
|
-
zh: '电子邮箱已确认!感谢。',
|
|
121
|
-
es: 'Correo electrónico confirmado! Gracias.',
|
|
122
|
-
},
|
|
123
|
-
};
|
|
124
|
-
const sendResult = await MailerProvider.send({
|
|
125
|
-
id,
|
|
126
|
-
sendOptions: {
|
|
127
|
-
to: req.body.email, // list of receivers
|
|
128
|
-
subject: translate.H1[req.lang], // Subject line
|
|
129
|
-
text: translate.H1[req.lang], // plain text body
|
|
130
|
-
html: MailerProvider.instance[id].templates.userVerifyEmail
|
|
131
|
-
.replace('{{H1}}', translate.H1[req.lang])
|
|
132
|
-
.replace('{{P1}}', translate.P1[req.lang])
|
|
133
|
-
.replace('{{TOKEN}}', token)
|
|
134
|
-
.replace(`{{COMPANY}}`, options.host), // html body
|
|
135
|
-
attachments: [
|
|
136
|
-
// {
|
|
137
|
-
// filename: 'logo.png',
|
|
138
|
-
// path: `./logo.png`,
|
|
139
|
-
// cid: 'logo', // <img src='cid:logo'>
|
|
140
|
-
// },
|
|
141
|
-
],
|
|
142
|
-
},
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
if (!sendResult) throw new Error('email send error');
|
|
146
|
-
return { message: 'email send successfully' };
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
switch (req.params.id) {
|
|
150
|
-
case 'auth':
|
|
151
|
-
const user = await User.findOne({
|
|
152
|
-
email: req.body.email,
|
|
153
|
-
});
|
|
154
|
-
const getMinutesRemaining = () => (-1 * user.failedLoginAttempts - new Date().getTime()) / (1000 * 60);
|
|
155
|
-
const accountLocketMessage = () =>
|
|
156
|
-
`Account locked. Please try again in: ${
|
|
157
|
-
getMinutesRemaining() < 1
|
|
158
|
-
? `${(getMinutesRemaining() * 60).toFixed(0)} s`
|
|
159
|
-
: `${getMinutesRemaining().toFixed(0)} min`
|
|
160
|
-
}.`;
|
|
161
|
-
|
|
162
|
-
if (user) {
|
|
163
|
-
const { _id } = user;
|
|
164
|
-
const validPassword = await verifyPassword(req.body.password, user.password);
|
|
165
|
-
if (validPassword === true) {
|
|
166
|
-
if (!user.profileImageId)
|
|
167
|
-
await User.findByIdAndUpdate(
|
|
168
|
-
user._id,
|
|
169
|
-
{ profileImageId: await getDefaultProfileImageId(File) },
|
|
170
|
-
{
|
|
171
|
-
runValidators: true,
|
|
172
|
-
},
|
|
173
|
-
);
|
|
174
|
-
{
|
|
175
|
-
if (getMinutesRemaining() <= 0 || user.failedLoginAttempts >= 0) {
|
|
176
|
-
const user = await User.findOne({
|
|
177
|
-
_id,
|
|
178
|
-
}).select(UserDto.select.get());
|
|
179
|
-
await User.findByIdAndUpdate(
|
|
180
|
-
_id,
|
|
181
|
-
{ lastLoginDate: new Date(), failedLoginAttempts: 0 },
|
|
182
|
-
{
|
|
183
|
-
runValidators: true,
|
|
184
|
-
},
|
|
185
|
-
);
|
|
186
|
-
return {
|
|
187
|
-
token: hashJWT({ user: UserDto.auth.payload(user) }),
|
|
188
|
-
user,
|
|
189
|
-
};
|
|
190
|
-
} else throw new Error(accountLocketMessage());
|
|
191
|
-
}
|
|
192
|
-
} else {
|
|
193
|
-
if (user.failedLoginAttempts >= 5) {
|
|
194
|
-
await User.findByIdAndUpdate(
|
|
195
|
-
_id,
|
|
196
|
-
{ failedLoginAttempts: -1 * (+new Date() + 60 * 1000 * 15) },
|
|
197
|
-
{
|
|
198
|
-
runValidators: true,
|
|
199
|
-
},
|
|
200
|
-
);
|
|
201
|
-
setTimeout(async () => {
|
|
202
|
-
await User.findByIdAndUpdate(
|
|
203
|
-
_id,
|
|
204
|
-
{ failedLoginAttempts: 0 },
|
|
205
|
-
{
|
|
206
|
-
runValidators: true,
|
|
207
|
-
},
|
|
208
|
-
);
|
|
209
|
-
}, 60 * 1000 * 15);
|
|
210
|
-
throw new Error(`Account locked. Please try again in: 15 min.`);
|
|
211
|
-
} else if (user.failedLoginAttempts < 0 && getMinutesRemaining() > 0) {
|
|
212
|
-
throw new Error(accountLocketMessage());
|
|
213
|
-
} else if (user.failedLoginAttempts < 0 && getMinutesRemaining() < 0) {
|
|
214
|
-
await User.findByIdAndUpdate(
|
|
215
|
-
_id,
|
|
216
|
-
{ failedLoginAttempts: 0 },
|
|
217
|
-
{
|
|
218
|
-
runValidators: true,
|
|
219
|
-
},
|
|
220
|
-
);
|
|
221
|
-
user.failedLoginAttempts = 0;
|
|
222
|
-
}
|
|
223
|
-
try {
|
|
224
|
-
await User.findByIdAndUpdate(
|
|
225
|
-
_id,
|
|
226
|
-
{ failedLoginAttempts: user.failedLoginAttempts + 1 },
|
|
227
|
-
{
|
|
228
|
-
runValidators: true,
|
|
229
|
-
},
|
|
230
|
-
);
|
|
231
|
-
} catch (error) {
|
|
232
|
-
logger.error(error, { params: req.params, body: req.body });
|
|
233
|
-
}
|
|
234
|
-
throw new Error(`invalid email or password, remaining attempts: ${5 - user.failedLoginAttempts}`);
|
|
235
|
-
}
|
|
236
|
-
} else throw new Error('invalid email or password');
|
|
237
|
-
|
|
238
|
-
default: {
|
|
239
|
-
const validatePassword = validatePasswordMiddleware(req.body.password);
|
|
240
|
-
if (validatePassword.status === 'error') throw new Error(validatePassword.message);
|
|
241
|
-
req.body.password = await hashPassword(req.body.password);
|
|
242
|
-
req.body.role = 'user';
|
|
243
|
-
req.body.profileImageId = await getDefaultProfileImageId(File);
|
|
244
|
-
const { _id } = await new User(req.body).save();
|
|
245
|
-
if (_id) {
|
|
246
|
-
const user = await User.findOne({ _id }).select(UserDto.select.get());
|
|
247
|
-
return {
|
|
248
|
-
token: hashJWT({ user: UserDto.auth.payload(user) }),
|
|
249
|
-
user,
|
|
250
|
-
};
|
|
251
|
-
} else throw new Error('failed to create user');
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
},
|
|
255
|
-
get: async (req, res, options) => {
|
|
256
|
-
/** @type {import('./user.model.js').UserModel} */
|
|
257
|
-
const User = DataBaseProvider.instance[`${options.host}${options.path}`].mongoose.User;
|
|
258
|
-
|
|
259
|
-
/** @type {import('../file/file.model.js').FileModel} */
|
|
260
|
-
const File = DataBaseProvider.instance[`${options.host}${options.path}`].mongoose.File;
|
|
261
|
-
|
|
262
|
-
if (req.path.startsWith('/email')) {
|
|
263
|
-
return await User.findOne({
|
|
264
|
-
email: req.params.email,
|
|
265
|
-
}).select(UserDto.select.get());
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
if (req.path.startsWith('/recover')) {
|
|
269
|
-
let payload;
|
|
270
|
-
try {
|
|
271
|
-
payload = verifyJWT(req.params.id);
|
|
272
|
-
} catch (error) {
|
|
273
|
-
logger.error(error, { 'req.params.id': req.params.id });
|
|
274
|
-
options.png.header(res);
|
|
275
|
-
return options.png.buffer['invalid-token'];
|
|
276
|
-
}
|
|
277
|
-
const user = await User.findOne({
|
|
278
|
-
email: payload.email,
|
|
279
|
-
});
|
|
280
|
-
if (user) {
|
|
281
|
-
const { _id } = user;
|
|
282
|
-
await User.findByIdAndUpdate(
|
|
283
|
-
_id,
|
|
284
|
-
{ recoverTimeOut: new Date(+new Date() + 1000 * 60 * 15) },
|
|
285
|
-
{ runValidators: true },
|
|
286
|
-
); // 15m
|
|
287
|
-
options.png.header(res);
|
|
288
|
-
return options.png.buffer['recover'];
|
|
289
|
-
} else {
|
|
290
|
-
options.png.header(res);
|
|
291
|
-
return options.png.buffer['invalid-token'];
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
if (req.path.startsWith('/mailer')) {
|
|
296
|
-
let payload;
|
|
297
|
-
try {
|
|
298
|
-
payload = verifyJWT(req.params.id);
|
|
299
|
-
} catch (error) {
|
|
300
|
-
logger.error(error, { 'req.params.id': req.params.id });
|
|
301
|
-
options.png.header(res);
|
|
302
|
-
return options.png.buffer['invalid-token'];
|
|
303
|
-
}
|
|
304
|
-
const user = await User.findOne({
|
|
305
|
-
email: payload.email,
|
|
306
|
-
});
|
|
307
|
-
if (user) {
|
|
308
|
-
const { _id } = user;
|
|
309
|
-
{
|
|
310
|
-
const user = await User.findByIdAndUpdate(_id, { emailConfirmed: true }, { runValidators: true });
|
|
311
|
-
}
|
|
312
|
-
const userWsId = CoreWsMailerManagement.getUserWsId(`${options.host}${options.path}`, user._id.toString());
|
|
313
|
-
CoreWsEmit(CoreWsMailerChannel.channel, CoreWsMailerChannel.client[userWsId], {
|
|
314
|
-
status: 'email-confirmed',
|
|
315
|
-
id: userWsId,
|
|
316
|
-
});
|
|
317
|
-
options.png.header(res);
|
|
318
|
-
return options.png.buffer['check'];
|
|
319
|
-
} else {
|
|
320
|
-
options.png.header(res);
|
|
321
|
-
return options.png.buffer['invalid-token'];
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
switch (req.params.id) {
|
|
326
|
-
case 'all':
|
|
327
|
-
return await User.find().select(UserDto.select.getAll());
|
|
328
|
-
|
|
329
|
-
case 'auth': {
|
|
330
|
-
const user = await User.findOne({
|
|
331
|
-
_id: req.auth.user._id,
|
|
332
|
-
});
|
|
333
|
-
|
|
334
|
-
const file = await File.findOne({ _id: user.profileImageId });
|
|
335
|
-
|
|
336
|
-
if (!file) {
|
|
337
|
-
await User.findByIdAndUpdate(
|
|
338
|
-
user._id,
|
|
339
|
-
{ profileImageId: await getDefaultProfileImageId(File) },
|
|
340
|
-
{
|
|
341
|
-
runValidators: true,
|
|
342
|
-
},
|
|
343
|
-
);
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
return await User.findOne({
|
|
347
|
-
_id: req.auth.user._id,
|
|
348
|
-
}).select(UserDto.select.get());
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
default: {
|
|
352
|
-
const user = await User.findOne({
|
|
353
|
-
_id: req.auth.user._id,
|
|
354
|
-
});
|
|
355
|
-
switch (user.role) {
|
|
356
|
-
case 'admin': {
|
|
357
|
-
if (req.params.id) return await User.findById(req.params.id);
|
|
358
|
-
return await User.find();
|
|
359
|
-
}
|
|
360
|
-
default:
|
|
361
|
-
if (req.auth.user._id !== req.params.id) throw new Error(`Invalid token user id`);
|
|
362
|
-
return await User.findOne({
|
|
363
|
-
_id: req.params.id,
|
|
364
|
-
}).select(UserDto.select.get());
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
}
|
|
368
|
-
},
|
|
369
|
-
delete: async (req, res, options) => {
|
|
370
|
-
/** @type {import('./user.model.js').UserModel} */
|
|
371
|
-
const User = DataBaseProvider.instance[`${options.host}${options.path}`].mongoose.User;
|
|
372
|
-
switch (req.params.id) {
|
|
373
|
-
default: {
|
|
374
|
-
const user = await User.findOne({
|
|
375
|
-
_id: req.auth.user._id,
|
|
376
|
-
});
|
|
377
|
-
switch (user.role) {
|
|
378
|
-
case 'admin': {
|
|
379
|
-
if (req.params.id) return await User.findByIdAndDelete(req.params.id);
|
|
380
|
-
else return await await User.deleteMany();
|
|
381
|
-
}
|
|
382
|
-
default:
|
|
383
|
-
if (req.auth.user._id !== req.params.id) throw new Error(`Invalid token user id`);
|
|
384
|
-
const user = await User.findOne({
|
|
385
|
-
_id: req.params.id,
|
|
386
|
-
}).select(UserDto.select.get());
|
|
387
|
-
if (user) {
|
|
388
|
-
await User.findByIdAndDelete(req.params.id);
|
|
389
|
-
return user;
|
|
390
|
-
} else throw new Error('user not found');
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
},
|
|
395
|
-
put: async (req, res, options) => {
|
|
396
|
-
/** @type {import('./user.model.js').UserModel} */
|
|
397
|
-
const User = DataBaseProvider.instance[`${options.host}${options.path}`].mongoose.User;
|
|
398
|
-
|
|
399
|
-
/** @type {import('../file/file.model.js').FileModel} */
|
|
400
|
-
const File = DataBaseProvider.instance[`${options.host}${options.path}`].mongoose.File;
|
|
401
|
-
|
|
402
|
-
// req.path | req.baseUrl
|
|
403
|
-
|
|
404
|
-
if (req.path.startsWith('/profile-image')) {
|
|
405
|
-
const _id = req.auth.user._id;
|
|
406
|
-
if (_id !== req.params.id) throw new Error(`Invalid token user id`);
|
|
407
|
-
const user = await User.findOne({
|
|
408
|
-
_id,
|
|
409
|
-
});
|
|
410
|
-
if (!user) throw new Error(`User not found`);
|
|
411
|
-
if (user.profileImageId) await File.findByIdAndDelete(user.profileImageId);
|
|
412
|
-
const [imageFile] = await FileFactory.upload(req, File);
|
|
413
|
-
if (!imageFile) throw new Error('invalid file');
|
|
414
|
-
await User.findByIdAndUpdate(
|
|
415
|
-
_id,
|
|
416
|
-
{
|
|
417
|
-
profileImageId: imageFile._id.toString(),
|
|
418
|
-
},
|
|
419
|
-
{ runValidators: true },
|
|
420
|
-
);
|
|
421
|
-
return await User.findOne({
|
|
422
|
-
_id,
|
|
423
|
-
}).select(UserDto.select.get());
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
if (req.path.startsWith('/recover')) {
|
|
427
|
-
const payload = verifyJWT(req.params.id);
|
|
428
|
-
const user = await User.findOne({
|
|
429
|
-
email: payload.email,
|
|
430
|
-
});
|
|
431
|
-
if (user && new Date().getTime() < new Date(user.recoverTimeOut).getTime()) {
|
|
432
|
-
const validatePassword = validatePasswordMiddleware(req.body.password);
|
|
433
|
-
if (validatePassword.status === 'error') throw new Error(validatePassword.message);
|
|
434
|
-
await User.findByIdAndUpdate(
|
|
435
|
-
user._id,
|
|
436
|
-
{ password: await hashPassword(req.body.password), recoverTimeOut: new Date(), failedLoginAttempts: 0 },
|
|
437
|
-
{ runValidators: true },
|
|
438
|
-
);
|
|
439
|
-
return await User.findOne({
|
|
440
|
-
_id: user._id,
|
|
441
|
-
}).select(UserDto.select.get());
|
|
442
|
-
} else throw new Error('invalid token');
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
switch (req.params.id) {
|
|
446
|
-
default: {
|
|
447
|
-
const user = await User.findOne({
|
|
448
|
-
_id: req.auth.user._id,
|
|
449
|
-
});
|
|
450
|
-
switch (user.role) {
|
|
451
|
-
case 'admin': {
|
|
452
|
-
if (req.body.password) req.body.password = await hashPassword(req.body.password);
|
|
453
|
-
return await User.findByIdAndUpdate(req.params.id, req.body, {
|
|
454
|
-
runValidators: true,
|
|
455
|
-
});
|
|
456
|
-
}
|
|
457
|
-
default: {
|
|
458
|
-
const _id = req.auth.user._id;
|
|
459
|
-
if (_id !== req.params.id) throw new Error(`Invalid token user id`);
|
|
460
|
-
const user = await User.findOne({ _id });
|
|
461
|
-
await User.findByIdAndUpdate(
|
|
462
|
-
_id,
|
|
463
|
-
{
|
|
464
|
-
email: req.body.email && !user.emailConfirmed ? req.body.email : user.email,
|
|
465
|
-
username: req.body.username,
|
|
466
|
-
},
|
|
467
|
-
{ runValidators: true },
|
|
468
|
-
);
|
|
469
|
-
return await User.findOne({
|
|
470
|
-
_id,
|
|
471
|
-
}).select(UserDto.select.get());
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
},
|
|
477
|
-
};
|
|
478
|
-
|
|
479
|
-
export { UserService };
|
|
1
|
+
import { loggerFactory } from '../../server/logger.js';
|
|
2
|
+
import { hashPassword, verifyPassword, hashJWT, verifyJWT, validatePasswordMiddleware } from '../../server/auth.js';
|
|
3
|
+
import { MailerProvider } from '../../mailer/MailerProvider.js';
|
|
4
|
+
import { CoreWsMailerManagement } from '../../ws/core/management/core.ws.mailer.js';
|
|
5
|
+
import { CoreWsEmit } from '../../ws/core/core.ws.emit.js';
|
|
6
|
+
import { CoreWsMailerChannel } from '../../ws/core/channels/core.ws.mailer.js';
|
|
7
|
+
import validator from 'validator';
|
|
8
|
+
import { DataBaseProvider } from '../../db/DataBaseProvider.js';
|
|
9
|
+
import { s4 } from '../../client/components/core/CommonJs.js';
|
|
10
|
+
import { FileFactory } from '../file/file.service.js';
|
|
11
|
+
import fs from 'fs-extra';
|
|
12
|
+
import { svg, png, png3x } from 'font-awesome-assets';
|
|
13
|
+
import { UserDto } from './user.model.js';
|
|
14
|
+
import Jimp from 'jimp';
|
|
15
|
+
|
|
16
|
+
const logger = loggerFactory(import.meta);
|
|
17
|
+
|
|
18
|
+
const getDefaultProfileImageId = async (File) => {
|
|
19
|
+
const faId = 'user';
|
|
20
|
+
const tmpFilePath = `./tmp/${faId}-${s4() + s4()}.svg`;
|
|
21
|
+
fs.writeFileSync(tmpFilePath, svg(faId, '#f5f5f5d1'), 'utf8');
|
|
22
|
+
const file = await new File(FileFactory.svg(fs.readFileSync(tmpFilePath), `${faId}.svg`)).save();
|
|
23
|
+
fs.removeSync(tmpFilePath);
|
|
24
|
+
return file._id;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const UserService = {
|
|
28
|
+
post: async (req, res, options) => {
|
|
29
|
+
/** @type {import('./user.model.js').UserModel} */
|
|
30
|
+
const User = DataBaseProvider.instance[`${options.host}${options.path}`].mongoose.User;
|
|
31
|
+
|
|
32
|
+
/** @type {import('../file/file.model.js').FileModel} */
|
|
33
|
+
const File = DataBaseProvider.instance[`${options.host}${options.path}`].mongoose.File;
|
|
34
|
+
|
|
35
|
+
if (req.params.id === 'recover-verify-email') {
|
|
36
|
+
const user = await User.findOne({
|
|
37
|
+
email: req.body.email,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
if (!user) throw new Error('Email address does not exist');
|
|
41
|
+
|
|
42
|
+
const token = hashJWT({ email: req.body.email }, '15m');
|
|
43
|
+
const payloadToken = hashJWT({ email: req.body.email }, '15m');
|
|
44
|
+
const id = `${options.host}${options.path}`;
|
|
45
|
+
const translate = {
|
|
46
|
+
H1: {
|
|
47
|
+
en: 'Recover your account',
|
|
48
|
+
es: 'Recupera tu cuenta',
|
|
49
|
+
},
|
|
50
|
+
P1: {
|
|
51
|
+
en: 'To recover your account, please click the button below:',
|
|
52
|
+
es: 'Para recuperar tu cuenta, haz click en el botón de abajo:',
|
|
53
|
+
},
|
|
54
|
+
BTN_LABEL: {
|
|
55
|
+
en: 'Recover Password',
|
|
56
|
+
es: 'Recuperar Contraseña',
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
const sendResult = await MailerProvider.send({
|
|
60
|
+
id,
|
|
61
|
+
sendOptions: {
|
|
62
|
+
to: req.body.email, // list of receivers
|
|
63
|
+
subject: translate.H1[req.lang], // Subject line
|
|
64
|
+
text: translate.H1[req.lang], // plain text body
|
|
65
|
+
html: MailerProvider.instance[id].templates.userRecoverEmail
|
|
66
|
+
.replace('{{H1}}', translate.H1[req.lang])
|
|
67
|
+
.replace('{{P1}}', translate.P1[req.lang])
|
|
68
|
+
.replace('{{TOKEN}}', token)
|
|
69
|
+
.replace(`{{COMPANY}}`, options.host) // html body
|
|
70
|
+
.replace(
|
|
71
|
+
'{{RECOVER_WEB_URL}}',
|
|
72
|
+
`${process.env === 'development' ? 'http://' : 'https://'}${options.host}${options.path}${
|
|
73
|
+
options.path === '/' ? 'recover' : `/recover`
|
|
74
|
+
}?payload=${payloadToken}`,
|
|
75
|
+
)
|
|
76
|
+
.replace('{{RECOVER_BTN_LABEL}}', translate.BTN_LABEL[req.lang]),
|
|
77
|
+
|
|
78
|
+
attachments: [
|
|
79
|
+
// {
|
|
80
|
+
// filename: 'logo.png',
|
|
81
|
+
// path: `./logo.png`,
|
|
82
|
+
// cid: 'logo', // <img src='cid:logo'>
|
|
83
|
+
// },
|
|
84
|
+
],
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
if (!sendResult) throw new Error('email send error');
|
|
89
|
+
return { message: 'email send successfully' };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (req.path.startsWith('/mailer') && req.params.id === 'verify-email') {
|
|
93
|
+
if (!validator.isEmail(req.body.email)) throw { message: 'invalid email' };
|
|
94
|
+
|
|
95
|
+
const token = hashJWT({ email: req.body.email });
|
|
96
|
+
const id = `${options.host}${options.path}`;
|
|
97
|
+
const user = await User.findById(req.auth.user._id);
|
|
98
|
+
|
|
99
|
+
if (user.emailConfirmed) throw new Error('email already confirmed');
|
|
100
|
+
|
|
101
|
+
if (user.email !== req.body.email) {
|
|
102
|
+
req.body.emailConfirmed = false;
|
|
103
|
+
|
|
104
|
+
const result = await User.findByIdAndUpdate(
|
|
105
|
+
req.auth.user._id,
|
|
106
|
+
{ emailConfirmed: false, email: req.body.email },
|
|
107
|
+
{
|
|
108
|
+
runValidators: true,
|
|
109
|
+
},
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
const translate = {
|
|
113
|
+
H1: {
|
|
114
|
+
en: 'Confirm Your Email',
|
|
115
|
+
zh: '请确认您的电子邮箱',
|
|
116
|
+
es: 'Confirma tu correo electrónico',
|
|
117
|
+
},
|
|
118
|
+
P1: {
|
|
119
|
+
en: 'Email confirmed! Thanks.',
|
|
120
|
+
zh: '电子邮箱已确认!感谢。',
|
|
121
|
+
es: 'Correo electrónico confirmado! Gracias.',
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
const sendResult = await MailerProvider.send({
|
|
125
|
+
id,
|
|
126
|
+
sendOptions: {
|
|
127
|
+
to: req.body.email, // list of receivers
|
|
128
|
+
subject: translate.H1[req.lang], // Subject line
|
|
129
|
+
text: translate.H1[req.lang], // plain text body
|
|
130
|
+
html: MailerProvider.instance[id].templates.userVerifyEmail
|
|
131
|
+
.replace('{{H1}}', translate.H1[req.lang])
|
|
132
|
+
.replace('{{P1}}', translate.P1[req.lang])
|
|
133
|
+
.replace('{{TOKEN}}', token)
|
|
134
|
+
.replace(`{{COMPANY}}`, options.host), // html body
|
|
135
|
+
attachments: [
|
|
136
|
+
// {
|
|
137
|
+
// filename: 'logo.png',
|
|
138
|
+
// path: `./logo.png`,
|
|
139
|
+
// cid: 'logo', // <img src='cid:logo'>
|
|
140
|
+
// },
|
|
141
|
+
],
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
if (!sendResult) throw new Error('email send error');
|
|
146
|
+
return { message: 'email send successfully' };
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
switch (req.params.id) {
|
|
150
|
+
case 'auth':
|
|
151
|
+
const user = await User.findOne({
|
|
152
|
+
email: req.body.email,
|
|
153
|
+
});
|
|
154
|
+
const getMinutesRemaining = () => (-1 * user.failedLoginAttempts - new Date().getTime()) / (1000 * 60);
|
|
155
|
+
const accountLocketMessage = () =>
|
|
156
|
+
`Account locked. Please try again in: ${
|
|
157
|
+
getMinutesRemaining() < 1
|
|
158
|
+
? `${(getMinutesRemaining() * 60).toFixed(0)} s`
|
|
159
|
+
: `${getMinutesRemaining().toFixed(0)} min`
|
|
160
|
+
}.`;
|
|
161
|
+
|
|
162
|
+
if (user) {
|
|
163
|
+
const { _id } = user;
|
|
164
|
+
const validPassword = await verifyPassword(req.body.password, user.password);
|
|
165
|
+
if (validPassword === true) {
|
|
166
|
+
if (!user.profileImageId)
|
|
167
|
+
await User.findByIdAndUpdate(
|
|
168
|
+
user._id,
|
|
169
|
+
{ profileImageId: await getDefaultProfileImageId(File) },
|
|
170
|
+
{
|
|
171
|
+
runValidators: true,
|
|
172
|
+
},
|
|
173
|
+
);
|
|
174
|
+
{
|
|
175
|
+
if (getMinutesRemaining() <= 0 || user.failedLoginAttempts >= 0) {
|
|
176
|
+
const user = await User.findOne({
|
|
177
|
+
_id,
|
|
178
|
+
}).select(UserDto.select.get());
|
|
179
|
+
await User.findByIdAndUpdate(
|
|
180
|
+
_id,
|
|
181
|
+
{ lastLoginDate: new Date(), failedLoginAttempts: 0 },
|
|
182
|
+
{
|
|
183
|
+
runValidators: true,
|
|
184
|
+
},
|
|
185
|
+
);
|
|
186
|
+
return {
|
|
187
|
+
token: hashJWT({ user: UserDto.auth.payload(user) }),
|
|
188
|
+
user,
|
|
189
|
+
};
|
|
190
|
+
} else throw new Error(accountLocketMessage());
|
|
191
|
+
}
|
|
192
|
+
} else {
|
|
193
|
+
if (user.failedLoginAttempts >= 5) {
|
|
194
|
+
await User.findByIdAndUpdate(
|
|
195
|
+
_id,
|
|
196
|
+
{ failedLoginAttempts: -1 * (+new Date() + 60 * 1000 * 15) },
|
|
197
|
+
{
|
|
198
|
+
runValidators: true,
|
|
199
|
+
},
|
|
200
|
+
);
|
|
201
|
+
setTimeout(async () => {
|
|
202
|
+
await User.findByIdAndUpdate(
|
|
203
|
+
_id,
|
|
204
|
+
{ failedLoginAttempts: 0 },
|
|
205
|
+
{
|
|
206
|
+
runValidators: true,
|
|
207
|
+
},
|
|
208
|
+
);
|
|
209
|
+
}, 60 * 1000 * 15);
|
|
210
|
+
throw new Error(`Account locked. Please try again in: 15 min.`);
|
|
211
|
+
} else if (user.failedLoginAttempts < 0 && getMinutesRemaining() > 0) {
|
|
212
|
+
throw new Error(accountLocketMessage());
|
|
213
|
+
} else if (user.failedLoginAttempts < 0 && getMinutesRemaining() < 0) {
|
|
214
|
+
await User.findByIdAndUpdate(
|
|
215
|
+
_id,
|
|
216
|
+
{ failedLoginAttempts: 0 },
|
|
217
|
+
{
|
|
218
|
+
runValidators: true,
|
|
219
|
+
},
|
|
220
|
+
);
|
|
221
|
+
user.failedLoginAttempts = 0;
|
|
222
|
+
}
|
|
223
|
+
try {
|
|
224
|
+
await User.findByIdAndUpdate(
|
|
225
|
+
_id,
|
|
226
|
+
{ failedLoginAttempts: user.failedLoginAttempts + 1 },
|
|
227
|
+
{
|
|
228
|
+
runValidators: true,
|
|
229
|
+
},
|
|
230
|
+
);
|
|
231
|
+
} catch (error) {
|
|
232
|
+
logger.error(error, { params: req.params, body: req.body });
|
|
233
|
+
}
|
|
234
|
+
throw new Error(`invalid email or password, remaining attempts: ${5 - user.failedLoginAttempts}`);
|
|
235
|
+
}
|
|
236
|
+
} else throw new Error('invalid email or password');
|
|
237
|
+
|
|
238
|
+
default: {
|
|
239
|
+
const validatePassword = validatePasswordMiddleware(req.body.password);
|
|
240
|
+
if (validatePassword.status === 'error') throw new Error(validatePassword.message);
|
|
241
|
+
req.body.password = await hashPassword(req.body.password);
|
|
242
|
+
req.body.role = 'user';
|
|
243
|
+
req.body.profileImageId = await getDefaultProfileImageId(File);
|
|
244
|
+
const { _id } = await new User(req.body).save();
|
|
245
|
+
if (_id) {
|
|
246
|
+
const user = await User.findOne({ _id }).select(UserDto.select.get());
|
|
247
|
+
return {
|
|
248
|
+
token: hashJWT({ user: UserDto.auth.payload(user) }),
|
|
249
|
+
user,
|
|
250
|
+
};
|
|
251
|
+
} else throw new Error('failed to create user');
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
},
|
|
255
|
+
get: async (req, res, options) => {
|
|
256
|
+
/** @type {import('./user.model.js').UserModel} */
|
|
257
|
+
const User = DataBaseProvider.instance[`${options.host}${options.path}`].mongoose.User;
|
|
258
|
+
|
|
259
|
+
/** @type {import('../file/file.model.js').FileModel} */
|
|
260
|
+
const File = DataBaseProvider.instance[`${options.host}${options.path}`].mongoose.File;
|
|
261
|
+
|
|
262
|
+
if (req.path.startsWith('/email')) {
|
|
263
|
+
return await User.findOne({
|
|
264
|
+
email: req.params.email,
|
|
265
|
+
}).select(UserDto.select.get());
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (req.path.startsWith('/recover')) {
|
|
269
|
+
let payload;
|
|
270
|
+
try {
|
|
271
|
+
payload = verifyJWT(req.params.id);
|
|
272
|
+
} catch (error) {
|
|
273
|
+
logger.error(error, { 'req.params.id': req.params.id });
|
|
274
|
+
options.png.header(res);
|
|
275
|
+
return options.png.buffer['invalid-token'];
|
|
276
|
+
}
|
|
277
|
+
const user = await User.findOne({
|
|
278
|
+
email: payload.email,
|
|
279
|
+
});
|
|
280
|
+
if (user) {
|
|
281
|
+
const { _id } = user;
|
|
282
|
+
await User.findByIdAndUpdate(
|
|
283
|
+
_id,
|
|
284
|
+
{ recoverTimeOut: new Date(+new Date() + 1000 * 60 * 15) },
|
|
285
|
+
{ runValidators: true },
|
|
286
|
+
); // 15m
|
|
287
|
+
options.png.header(res);
|
|
288
|
+
return options.png.buffer['recover'];
|
|
289
|
+
} else {
|
|
290
|
+
options.png.header(res);
|
|
291
|
+
return options.png.buffer['invalid-token'];
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (req.path.startsWith('/mailer')) {
|
|
296
|
+
let payload;
|
|
297
|
+
try {
|
|
298
|
+
payload = verifyJWT(req.params.id);
|
|
299
|
+
} catch (error) {
|
|
300
|
+
logger.error(error, { 'req.params.id': req.params.id });
|
|
301
|
+
options.png.header(res);
|
|
302
|
+
return options.png.buffer['invalid-token'];
|
|
303
|
+
}
|
|
304
|
+
const user = await User.findOne({
|
|
305
|
+
email: payload.email,
|
|
306
|
+
});
|
|
307
|
+
if (user) {
|
|
308
|
+
const { _id } = user;
|
|
309
|
+
{
|
|
310
|
+
const user = await User.findByIdAndUpdate(_id, { emailConfirmed: true }, { runValidators: true });
|
|
311
|
+
}
|
|
312
|
+
const userWsId = CoreWsMailerManagement.getUserWsId(`${options.host}${options.path}`, user._id.toString());
|
|
313
|
+
CoreWsEmit(CoreWsMailerChannel.channel, CoreWsMailerChannel.client[userWsId], {
|
|
314
|
+
status: 'email-confirmed',
|
|
315
|
+
id: userWsId,
|
|
316
|
+
});
|
|
317
|
+
options.png.header(res);
|
|
318
|
+
return options.png.buffer['check'];
|
|
319
|
+
} else {
|
|
320
|
+
options.png.header(res);
|
|
321
|
+
return options.png.buffer['invalid-token'];
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
switch (req.params.id) {
|
|
326
|
+
case 'all':
|
|
327
|
+
return await User.find().select(UserDto.select.getAll());
|
|
328
|
+
|
|
329
|
+
case 'auth': {
|
|
330
|
+
const user = await User.findOne({
|
|
331
|
+
_id: req.auth.user._id,
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
const file = await File.findOne({ _id: user.profileImageId });
|
|
335
|
+
|
|
336
|
+
if (!file) {
|
|
337
|
+
await User.findByIdAndUpdate(
|
|
338
|
+
user._id,
|
|
339
|
+
{ profileImageId: await getDefaultProfileImageId(File) },
|
|
340
|
+
{
|
|
341
|
+
runValidators: true,
|
|
342
|
+
},
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
return await User.findOne({
|
|
347
|
+
_id: req.auth.user._id,
|
|
348
|
+
}).select(UserDto.select.get());
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
default: {
|
|
352
|
+
const user = await User.findOne({
|
|
353
|
+
_id: req.auth.user._id,
|
|
354
|
+
});
|
|
355
|
+
switch (user.role) {
|
|
356
|
+
case 'admin': {
|
|
357
|
+
if (req.params.id) return await User.findById(req.params.id);
|
|
358
|
+
return await User.find();
|
|
359
|
+
}
|
|
360
|
+
default:
|
|
361
|
+
if (req.auth.user._id !== req.params.id) throw new Error(`Invalid token user id`);
|
|
362
|
+
return await User.findOne({
|
|
363
|
+
_id: req.params.id,
|
|
364
|
+
}).select(UserDto.select.get());
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
},
|
|
369
|
+
delete: async (req, res, options) => {
|
|
370
|
+
/** @type {import('./user.model.js').UserModel} */
|
|
371
|
+
const User = DataBaseProvider.instance[`${options.host}${options.path}`].mongoose.User;
|
|
372
|
+
switch (req.params.id) {
|
|
373
|
+
default: {
|
|
374
|
+
const user = await User.findOne({
|
|
375
|
+
_id: req.auth.user._id,
|
|
376
|
+
});
|
|
377
|
+
switch (user.role) {
|
|
378
|
+
case 'admin': {
|
|
379
|
+
if (req.params.id) return await User.findByIdAndDelete(req.params.id);
|
|
380
|
+
else return await await User.deleteMany();
|
|
381
|
+
}
|
|
382
|
+
default:
|
|
383
|
+
if (req.auth.user._id !== req.params.id) throw new Error(`Invalid token user id`);
|
|
384
|
+
const user = await User.findOne({
|
|
385
|
+
_id: req.params.id,
|
|
386
|
+
}).select(UserDto.select.get());
|
|
387
|
+
if (user) {
|
|
388
|
+
await User.findByIdAndDelete(req.params.id);
|
|
389
|
+
return user;
|
|
390
|
+
} else throw new Error('user not found');
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
},
|
|
395
|
+
put: async (req, res, options) => {
|
|
396
|
+
/** @type {import('./user.model.js').UserModel} */
|
|
397
|
+
const User = DataBaseProvider.instance[`${options.host}${options.path}`].mongoose.User;
|
|
398
|
+
|
|
399
|
+
/** @type {import('../file/file.model.js').FileModel} */
|
|
400
|
+
const File = DataBaseProvider.instance[`${options.host}${options.path}`].mongoose.File;
|
|
401
|
+
|
|
402
|
+
// req.path | req.baseUrl
|
|
403
|
+
|
|
404
|
+
if (req.path.startsWith('/profile-image')) {
|
|
405
|
+
const _id = req.auth.user._id;
|
|
406
|
+
if (_id !== req.params.id) throw new Error(`Invalid token user id`);
|
|
407
|
+
const user = await User.findOne({
|
|
408
|
+
_id,
|
|
409
|
+
});
|
|
410
|
+
if (!user) throw new Error(`User not found`);
|
|
411
|
+
if (user.profileImageId) await File.findByIdAndDelete(user.profileImageId);
|
|
412
|
+
const [imageFile] = await FileFactory.upload(req, File);
|
|
413
|
+
if (!imageFile) throw new Error('invalid file');
|
|
414
|
+
await User.findByIdAndUpdate(
|
|
415
|
+
_id,
|
|
416
|
+
{
|
|
417
|
+
profileImageId: imageFile._id.toString(),
|
|
418
|
+
},
|
|
419
|
+
{ runValidators: true },
|
|
420
|
+
);
|
|
421
|
+
return await User.findOne({
|
|
422
|
+
_id,
|
|
423
|
+
}).select(UserDto.select.get());
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (req.path.startsWith('/recover')) {
|
|
427
|
+
const payload = verifyJWT(req.params.id);
|
|
428
|
+
const user = await User.findOne({
|
|
429
|
+
email: payload.email,
|
|
430
|
+
});
|
|
431
|
+
if (user && new Date().getTime() < new Date(user.recoverTimeOut).getTime()) {
|
|
432
|
+
const validatePassword = validatePasswordMiddleware(req.body.password);
|
|
433
|
+
if (validatePassword.status === 'error') throw new Error(validatePassword.message);
|
|
434
|
+
await User.findByIdAndUpdate(
|
|
435
|
+
user._id,
|
|
436
|
+
{ password: await hashPassword(req.body.password), recoverTimeOut: new Date(), failedLoginAttempts: 0 },
|
|
437
|
+
{ runValidators: true },
|
|
438
|
+
);
|
|
439
|
+
return await User.findOne({
|
|
440
|
+
_id: user._id,
|
|
441
|
+
}).select(UserDto.select.get());
|
|
442
|
+
} else throw new Error('invalid token');
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
switch (req.params.id) {
|
|
446
|
+
default: {
|
|
447
|
+
const user = await User.findOne({
|
|
448
|
+
_id: req.auth.user._id,
|
|
449
|
+
});
|
|
450
|
+
switch (user.role) {
|
|
451
|
+
case 'admin': {
|
|
452
|
+
if (req.body.password) req.body.password = await hashPassword(req.body.password);
|
|
453
|
+
return await User.findByIdAndUpdate(req.params.id, req.body, {
|
|
454
|
+
runValidators: true,
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
default: {
|
|
458
|
+
const _id = req.auth.user._id;
|
|
459
|
+
if (_id !== req.params.id) throw new Error(`Invalid token user id`);
|
|
460
|
+
const user = await User.findOne({ _id });
|
|
461
|
+
await User.findByIdAndUpdate(
|
|
462
|
+
_id,
|
|
463
|
+
{
|
|
464
|
+
email: req.body.email && !user.emailConfirmed ? req.body.email : user.email,
|
|
465
|
+
username: req.body.username,
|
|
466
|
+
},
|
|
467
|
+
{ runValidators: true },
|
|
468
|
+
);
|
|
469
|
+
return await User.findOne({
|
|
470
|
+
_id,
|
|
471
|
+
}).select(UserDto.select.get());
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
},
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
export { UserService };
|