UNPKG

3.78 kBPlain TextView Raw
1import { providerCallback } from './endpoints/oauth/provider-callback';
2import { resetPassword, sendResetPasswordEmail } from './endpoints/password/reset';
3import { verifyEmail, sendVerificationEmail } from './endpoints/password/verify-email';
4import * as express from 'express';
5import * as requestIp from 'request-ip';
6import { AccountsServer } from '@accounts/server';
7import { refreshAccessToken } from './endpoints/refresh-access-token';
8import { getUser } from './endpoints/get-user';
9import { impersonate } from './endpoints/impersonate';
10import { logout } from './endpoints/logout';
11import { serviceAuthenticate } from './endpoints/service-authenticate';
12import { serviceVerifyAuthentication } from './endpoints/verify-authentication';
13import { registerPassword } from './endpoints/password/register';
14import { twoFactorSecret, twoFactorSet, twoFactorUnset } from './endpoints/password/two-factor';
15import { changePassword } from './endpoints/password/change-password';
16import { addEmail } from './endpoints/password/add-email';
17import { userLoader } from './user-loader';
18import { AccountsExpressOptions } from './types';
19import { getUserAgent } from './utils/get-user-agent';
20
21const defaultOptions: AccountsExpressOptions = {
22 path: '/accounts',
23};
24
25const accountsExpress = (
26 accountsServer: AccountsServer,
27 options: AccountsExpressOptions = {}
28): express.Router => {
29 options = { ...defaultOptions, ...options };
30 let { path } = options;
31
32 // Stop invalid double slash root path
33 if (path === '/') {
34 path = '';
35 }
36
37 const router = express.Router();
38
39 /**
40 * Middleware to populate the user agent and ip.
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 // @accounts/password
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 // @accounts/oauth
108 if (services.oauth) {
109 router.get(`${path}/oauth/:provider/callback`, providerCallback(accountsServer, options));
110 }
111
112 return router;
113};
114
115export default accountsExpress;