Commit 3a7ff38c authored by Grant's avatar Grant
Browse files

add error handlers

parent a8dfbaf8
Loading
Loading
Loading
Loading
+33 −14
Original line number Diff line number Diff line
@@ -155,6 +155,7 @@ app.post("/login/step/username", async (req, res) => {
      });
      req.session.login.session_id = session.id;

      try {
        await deliveryProvider!.send([username, instance], "code: " + code);

        req.session.save(() => {
@@ -170,6 +171,24 @@ app.post("/login/step/username", async (req, res) => {
            },
          });
        });
      } 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;
    }
+19 −6
Original line number Diff line number Diff line
@@ -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,6 +45,7 @@ app.use("/interaction/:uid", async (req, res, next) => {
    if (typeof req.session.user === "undefined") {
      res.redirect("/login?return=" + encodeURIComponent(req.originalUrl));
    } else {
      try {
        const returnTo = await oidc.interactionResult(req, res, {
          login: { accountId: req.session.user.sub },
        });
@@ -51,6 +53,17 @@ app.use("/interaction/:uid", async (req, res, next) => {
        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;