1 | import { providerCallback } from './endpoints/oauth/provider-callback';
|
2 | import { resetPassword, sendResetPasswordEmail } from './endpoints/password/reset';
|
3 | import { verifyEmail, sendVerificationEmail } from './endpoints/password/verify-email';
|
4 | import * as express from 'express';
|
5 | import * as requestIp from 'request-ip';
|
6 | import { AccountsServer } from '@accounts/server';
|
7 | import { refreshAccessToken } from './endpoints/refresh-access-token';
|
8 | import { getUser } from './endpoints/get-user';
|
9 | import { impersonate } from './endpoints/impersonate';
|
10 | import { logout } from './endpoints/logout';
|
11 | import { serviceAuthenticate } from './endpoints/service-authenticate';
|
12 | import { serviceVerifyAuthentication } from './endpoints/verify-authentication';
|
13 | import { registerPassword } from './endpoints/password/register';
|
14 | import { twoFactorSecret, twoFactorSet, twoFactorUnset } from './endpoints/password/two-factor';
|
15 | import { changePassword } from './endpoints/password/change-password';
|
16 | import { addEmail } from './endpoints/password/add-email';
|
17 | import { userLoader } from './user-loader';
|
18 | import { AccountsExpressOptions } from './types';
|
19 | import { getUserAgent } from './utils/get-user-agent';
|
20 |
|
21 | const defaultOptions: AccountsExpressOptions = {
|
22 | path: '/accounts',
|
23 | };
|
24 |
|
25 | const accountsExpress = (
|
26 | accountsServer: AccountsServer,
|
27 | options: AccountsExpressOptions = {}
|
28 | ): express.Router => {
|
29 | options = { ...defaultOptions, ...options };
|
30 | let { path } = options;
|
31 |
|
32 |
|
33 | if (path === '/') {
|
34 | path = '';
|
35 | }
|
36 |
|
37 | const router = express.Router();
|
38 |
|
39 | |
40 |
|
41 |
|
42 | router.use((req, _, next) => {
|
43 | const userAgent = getUserAgent(req);
|
44 | const ip = requestIp.getClientIp(req)!;
|
45 | req.infos = {
|
46 | userAgent,
|
47 | ip,
|
48 | };
|
49 |
|
50 | next();
|
51 | });
|
52 |
|
53 | router.post(`${path}/impersonate`, impersonate(accountsServer));
|
54 |
|
55 | router.get(`${path}/user`, userLoader(accountsServer), getUser());
|
56 | router.post(`${path}/user`, userLoader(accountsServer), getUser());
|
57 |
|
58 | router.post(`${path}/refreshTokens`, refreshAccessToken(accountsServer));
|
59 |
|
60 | router.post(`${path}/logout`, userLoader(accountsServer), logout(accountsServer));
|
61 |
|
62 | router.post(`${path}/:service/verifyAuthentication`, serviceVerifyAuthentication(accountsServer));
|
63 |
|
64 | router.post(`${path}/:service/authenticate`, serviceAuthenticate(accountsServer));
|
65 |
|
66 | const services = accountsServer.getServices();
|
67 |
|
68 |
|
69 | if (services.password) {
|
70 | router.post(`${path}/password/register`, registerPassword(accountsServer));
|
71 |
|
72 | router.post(`${path}/password/verifyEmail`, verifyEmail(accountsServer));
|
73 |
|
74 | router.post(`${path}/password/resetPassword`, resetPassword(accountsServer));
|
75 |
|
76 | router.post(`${path}/password/sendVerificationEmail`, sendVerificationEmail(accountsServer));
|
77 |
|
78 | router.post(`${path}/password/sendResetPasswordEmail`, sendResetPasswordEmail(accountsServer));
|
79 |
|
80 | router.post(`${path}/password/addEmail`, userLoader(accountsServer), addEmail(accountsServer));
|
81 |
|
82 | router.post(
|
83 | `${path}/password/changePassword`,
|
84 | userLoader(accountsServer),
|
85 | changePassword(accountsServer)
|
86 | );
|
87 |
|
88 | router.post(
|
89 | `${path}/password/twoFactorSecret`,
|
90 | userLoader(accountsServer),
|
91 | twoFactorSecret(accountsServer)
|
92 | );
|
93 |
|
94 | router.post(
|
95 | `${path}/password/twoFactorSet`,
|
96 | userLoader(accountsServer),
|
97 | twoFactorSet(accountsServer)
|
98 | );
|
99 |
|
100 | router.post(
|
101 | `${path}/password/twoFactorUnset`,
|
102 | userLoader(accountsServer),
|
103 | twoFactorUnset(accountsServer)
|
104 | );
|
105 | }
|
106 |
|
107 |
|
108 | if (services.oauth) {
|
109 | router.get(`${path}/oauth/:provider/callback`, providerCallback(accountsServer, options));
|
110 | }
|
111 |
|
112 | return router;
|
113 | };
|
114 |
|
115 | export default accountsExpress;
|