Skip to content

Commit

Permalink
add try-catch to demo auth middleware (#1044)
Browse files Browse the repository at this point in the history
- Since we validate email used in auth the route function needs to
  handle the possibility that userService.loginUserWithoutPassword can
  throw.
  • Loading branch information
Christopher Kolstad committed Oct 19, 2021
1 parent 72445b6 commit 28d0238
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/lib/middleware/demo-authentication.ts
Expand Up @@ -9,16 +9,25 @@ function demoAuthentication(
): void {
app.post(`${basePath}/api/admin/login`, async (req, res) => {
const { email } = req.body;
const user = await userService.loginUserWithoutPassword(email, true);
const session = req.session || {};
// @ts-ignore
session.user = user;
// @ts-ignore
req.session = session;
res.status(200)
try {
const user = await userService.loginUserWithoutPassword(
email,
true,
);
const session = req.session || {};
// @ts-ignore
.json(req.session.user)
.end();
session.user = user;
// @ts-ignore
req.session = session;
res.status(200)
// @ts-ignore
.json(req.session.user)
.end();
} catch (e) {
res.status(400)
.json({ error: `Could not sign in with ${email}` })
.end();
}
});

app.use(`${basePath}/api/admin/`, (req, res, next) => {
Expand Down

0 comments on commit 28d0238

Please sign in to comment.