Skip to content

Commit

Permalink
fix forward auth
Browse files Browse the repository at this point in the history
  • Loading branch information
NextFire committed Apr 10, 2024
1 parent c4beb61 commit 92955f1
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 9 deletions.
3 changes: 2 additions & 1 deletion server/api/discord/callback.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ export default eventHandler(async (event) => {
assert(typeof code === "string");

const session = await useTypedSession(event);
const redirect = session.data.redirect || "/";
const redirect = session.data.redirect || "/_oauth";

const params = new URLSearchParams({ code });
return sendRedirect(event, `${redirect}?${params}`);
});
9 changes: 4 additions & 5 deletions server/routes/_oauth.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import assert from "node:assert/strict";
export default eventHandler(async (event) => {
const session = await useTypedSession(event);
const query = getQuery(event);
const { code, forwardAuthRedirect } = query;
if (forwardAuthRedirect) {
assert(typeof forwardAuthRedirect === "string");
await session.update({ redirect: ".", forwardAuthRedirect });
const { code, proto, host, uri } = query;
if (host) {
await session.update({ redirect: `${proto}://${host}${uri}` });
return sendRedirect(event, `${userConfig.publicUrl}/api/discord/auth`);
} else if (code) {
assert(typeof code === "string");
Expand All @@ -23,7 +22,7 @@ export default eventHandler(async (event) => {
forwardAuthGroups: groups,
forwardAuthExpires: Date.now() + 1000 * 60 * 60 * 24,
});
return sendRedirect(event, session.data.forwardAuthRedirect);
return sendRedirect(event, session.data.redirect || "/");
} else {
throw createError({ status: 400, message: "Missing required query" });
}
Expand Down
3 changes: 1 addition & 2 deletions server/routes/traefik.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ export default eventHandler(async (event) => {
throw createError({ status: 401, message: "Missing required group" });
}
} else {
const forwardAuthRedirect = `${proto}://${host}${uri}`;
const params = new URLSearchParams({ forwardAuthRedirect });
const params = new URLSearchParams({ proto, host, uri });
return sendRedirect(event, `http://${host}/_oauth?${params}`);
}
});
1 change: 0 additions & 1 deletion server/utils/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { type H3Event } from "h3";

interface SessionData {
redirect: string;
forwardAuthRedirect: string;
forwardAuthGroups: string[];
forwardAuthExpires: number;
}
Expand Down

0 comments on commit 92955f1

Please sign in to comment.