Skip to content

Scheduling infrastructure for absolutely everyone.

fix issue where user could not connect to caldav

femyeda49e7cbf8034a
diff --git a/lib/integrations/CalDav/CalDavCalendarAdapter.ts b/lib/integrations/CalDav/CalDavCalendarAdapter.ts
index 9adc675ea08..1e76c0bf866 100644
--- a/lib/integrations/CalDav/CalDavCalendarAdapter.ts
+++ b/lib/integrations/CalDav/CalDavCalendarAdapter.ts
@@ -103,6 +103,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
};
} catch (reason) {
console.error(reason);
+ throw reason;
}
}
@@ -153,6 +154,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
return null;
} catch (reason) {
console.error(reason);
+ throw reason;
}
}
@@ -186,6 +188,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
return null;
} catch (reason) {
console.error(reason);
+ throw reason;
}
}
@@ -212,6 +215,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
return events;
} catch (reason) {
console.error(reason);
+ throw reason;
}
}
@@ -235,6 +239,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
}));
} catch (reason) {
console.error(reason);
+ throw reason;
}
}
@@ -295,6 +300,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
return events;
} catch (reason) {
console.error(reason);
+ throw reason;
}
}
diff --git a/pages/api/integrations/caldav/add.ts b/pages/api/integrations/caldav/add.ts
index 843735ba5b1..06b2bdddf75 100644
--- a/pages/api/integrations/caldav/add.ts
+++ b/pages/api/integrations/caldav/add.ts
@@ -3,7 +3,7 @@ import { getSession } from "next-auth/client";
import prisma from "../../../../lib/prisma";
import { symmetricEncrypt } from "@lib/crypto";
import logger from "@lib/logger";
-import { davRequest, getBasicAuthHeaders } from "tsdav";
+import { CalDavCalendar } from "@lib/integrations/CalDav/CalDavCalendarAdapter";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === "POST") {
@@ -26,47 +26,28 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
},
});
- const header = getBasicAuthHeaders({
- username,
- password,
- });
-
try {
- const [response] = await davRequest({
- url: url,
- init: {
- method: "PROPFIND",
- namespace: "d",
- body: {
- propfind: {
- _attributes: {
- "xmlns:d": "DAV:",
- },
- prop: { "d:current-user-principal": {} },
- },
- },
- headers: header,
- },
+ const dav = new CalDavCalendar({
+ id: 0,
+ type: "caldav_calendar",
+ key: symmetricEncrypt(
+ JSON.stringify({ username, password, url }),
+ process.env.CALENDSO_ENCRYPTION_KEY
+ ),
+ userId: session.user.id,
});
- if (!response.ok) {
- logger.error("Could not add this caldav account", response?.statusText);
- logger.error(response.error);
- return res.status(200).json({ message: "Could not add this caldav account" });
- }
-
- if (response.ok) {
- await prisma.credential.create({
- data: {
- type: "caldav_calendar",
- key: symmetricEncrypt(
- JSON.stringify({ username, password, url }),
- process.env.CALENDSO_ENCRYPTION_KEY
- ),
- userId: session.user.id,
- },
- });
- }
+ await dav.listCalendars();
+ await prisma.credential.create({
+ data: {
+ type: "caldav_calendar",
+ key: symmetricEncrypt(
+ JSON.stringify({ username, password, url }),
+ process.env.CALENDSO_ENCRYPTION_KEY
+ ),
+ userId: session.user.id,
+ },
+ });
} catch (reason) {
logger.error("Could not add this caldav account", reason);
return res.status(500).json({ message: "Could not add this caldav account" });