Skip to content

Commit 26a0fcf

Browse files
committed
fix: rest endpoints should take either an instance or an injector as parameter
1 parent e3aba87 commit 26a0fcf

File tree

1 file changed

+61
-49
lines changed

1 file changed

+61
-49
lines changed

packages/password/src/endpoints/express.ts

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,64 +30,76 @@ export const infosMiddleware = (req: Request, _res: Response, next: NextFunction
3030
next();
3131
};
3232

33-
export const verifyEmail = (injector: Injector) => async (req: Request, res: Response) => {
34-
try {
35-
const { token } = req.params;
36-
if (token == null) {
37-
throw new Error('Token is missing');
38-
}
39-
await injector.get(AccountsPassword).verifyEmail(token);
40-
res.send(
41-
getHtml(
42-
'Email successfully verified',
43-
`
33+
export const verifyEmail =
34+
(accountsPasswordOrInjector: Injector | AccountsPassword) =>
35+
async (req: Request, res: Response) => {
36+
try {
37+
const { token } = req.params;
38+
if (token == null) {
39+
throw new Error('Token is missing');
40+
}
41+
const accountsPassword =
42+
accountsPasswordOrInjector instanceof AccountsPassword
43+
? accountsPasswordOrInjector
44+
: accountsPasswordOrInjector.get(AccountsPassword);
45+
await accountsPassword.verifyEmail(token);
46+
res.send(
47+
getHtml(
48+
'Email successfully verified',
49+
`
4450
<h3>The email address has been successfully verified.</h3>
4551
`
46-
)
47-
);
48-
} catch (err: any) {
49-
res.send(
50-
//codeql[js/xss-through-exception]
51-
getHtml(
52-
'Email verification error',
53-
`
52+
)
53+
);
54+
} catch (err: any) {
55+
res.send(
56+
//codeql[js/xss-through-exception]
57+
getHtml(
58+
'Email verification error',
59+
`
5460
<h3>The email address couldn't be verified: ${err.message ?? 'unknown error'}</h3>
5561
`
56-
)
57-
);
58-
}
59-
};
60-
61-
export const resetPassword = (injector: Injector) => async (req: Request, res: Response) => {
62-
try {
63-
const { token, newPassword } = req.body;
64-
if (token == null) {
65-
throw new Error('Token is missing');
66-
}
67-
if (newPassword == null) {
68-
throw new Error('New password is missing');
62+
)
63+
);
6964
}
70-
await injector.get(AccountsPassword).resetPassword(token, newPassword, req.infos);
71-
res.send(
72-
getHtml(
73-
'Password successfully changed',
74-
`
65+
};
66+
67+
export const resetPassword =
68+
(accountsPasswordOrInjector: Injector | AccountsPassword) =>
69+
async (req: Request, res: Response) => {
70+
try {
71+
const { token, newPassword } = req.body;
72+
if (token == null) {
73+
throw new Error('Token is missing');
74+
}
75+
if (newPassword == null) {
76+
throw new Error('New password is missing');
77+
}
78+
const accountsPassword =
79+
accountsPasswordOrInjector instanceof AccountsPassword
80+
? accountsPasswordOrInjector
81+
: accountsPasswordOrInjector.get(AccountsPassword);
82+
await accountsPassword.resetPassword(token, newPassword, req.infos);
83+
res.send(
84+
getHtml(
85+
'Password successfully changed',
86+
`
7587
<h3>The password has been successfully changed.</h3>
7688
`
77-
)
78-
);
79-
} catch (err: any) {
80-
//codeql[js/xss-through-exception]
81-
res.send(
82-
getHtml(
83-
'Password reset error',
84-
`
89+
)
90+
);
91+
} catch (err: any) {
92+
//codeql[js/xss-through-exception]
93+
res.send(
94+
getHtml(
95+
'Password reset error',
96+
`
8597
<h3>The password couldn't be changed: ${err.message ?? 'unknown error'}</h3>
8698
`
87-
)
88-
);
89-
}
90-
};
99+
)
100+
);
101+
}
102+
};
91103

92104
export const resetPasswordForm = (req: Request, res: Response): Response =>
93105
res.send(

0 commit comments

Comments
 (0)