add error handlers

This commit is contained in:
Grant 2024-07-14 03:52:12 -06:00
parent a8dfbaf8ea
commit 3a7ff38c0d
2 changed files with 51 additions and 19 deletions

View File

@ -155,21 +155,40 @@ app.post("/login/step/username", async (req, res) => {
});
req.session.login.session_id = session.id;
await deliveryProvider!.send([username, instance], "code: " + code);
try {
await deliveryProvider!.send([username, instance], "code: " + code);
req.session.save(() => {
res.send({
success: true,
step: "CODE_SENT",
data: {
session_id: session.id,
account:
deliveryProvider!.service_account.username +
"@" +
deliveryProvider!.service_account.host,
},
req.session.save(() => {
res.send({
success: true,
step: "CODE_SENT",
data: {
session_id: session.id,
account:
deliveryProvider!.service_account.username +
"@" +
deliveryProvider!.service_account.host,
},
});
});
});
} catch (e) {
console.error(
"Error while delivering to " + [username, instance].join("@"),
e
);
await prisma.authSession.delete({ where: { id: session.id } });
req.session.login.session_id = undefined;
req.session.save(() => {
res.send({
success: false,
error:
"Error while sending: " +
((e as any)?.message || "unknown error"),
});
});
}
break;
}

View File

@ -5,6 +5,7 @@ import bodyParser from "body-parser";
import path from "path";
import { oidc } from "./oidc.js";
import { makeClientPublic } from "./utils.js";
import { errors as OIDC_Errors } from "oidc-provider";
import "../types/session-types.js";
import { APIRouter } from "./api.js";
@ -44,13 +45,25 @@ app.use("/interaction/:uid", async (req, res, next) => {
if (typeof req.session.user === "undefined") {
res.redirect("/login?return=" + encodeURIComponent(req.originalUrl));
} else {
const returnTo = await oidc.interactionResult(req, res, {
login: { accountId: req.session.user.sub },
});
try {
const returnTo = await oidc.interactionResult(req, res, {
login: { accountId: req.session.user.sub },
});
req.session.destroy(() => {
res.redirect(returnTo);
});
req.session.destroy(() => {
res.redirect(returnTo);
});
} catch (e) {
console.error("Error while in interaction middleware", e);
req.session.destroy(() => {
if (e instanceof OIDC_Errors.SessionNotFound) {
res.send("<h1>session lost</h1>try logging in again");
} else {
res.send("<h1>unknown error</h1> try logging in again");
}
});
}
}
return;