Skip to content

Commit

Permalink
fix redirects with query params
Browse files Browse the repository at this point in the history
  • Loading branch information
NextFire committed Apr 10, 2024
1 parent 92955f1 commit 1a658dd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
11 changes: 10 additions & 1 deletion server/api/discord/auth.get.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
export default eventHandler((event) => {
import assert from "node:assert/strict";

export default eventHandler(async (event) => {
const query = getQuery(event);
const { redirect } = query;
assert(typeof redirect === "string");

const session = await useTypedSession(event);
await session.update({ redirect });

const authUrl = getAuthorizationUrl({
scope: "identify email guilds guilds.members.read",
prompt: "none",
Expand Down
2 changes: 1 addition & 1 deletion server/api/discord/callback.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default eventHandler(async (event) => {
assert(typeof code === "string");

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

const params = new URLSearchParams({ code });
return sendRedirect(event, `${redirect}?${params}`);
Expand Down
8 changes: 7 additions & 1 deletion server/routes/_oauth.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ export default eventHandler(async (event) => {
const { code, proto, host, uri } = query;
if (host) {
await session.update({ redirect: `${proto}://${host}${uri}` });
return sendRedirect(event, `${userConfig.publicUrl}/api/discord/auth`);
const params = new URLSearchParams({
redirect: `http://${host}/${event.path}`,
});
return sendRedirect(
event,
`${userConfig.publicUrl}/api/discord/auth${params}`
);
} else if (code) {
assert(typeof code === "string");
const resp = await exchangeCode(code);
Expand Down
6 changes: 2 additions & 4 deletions server/routes/interaction/[uid]/login.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ export default eventHandler(async (event) => {
const interaction = await provider.interactionDetails(req, res);
assert.equal(interaction.prompt.name, "login");

const session = await useTypedSession(event);
await session.update({ redirect: `${event.path}/../callback` });

return sendRedirect(event, "/api/discord/auth");
const params = new URLSearchParams({ redirect: `${event.path}/../callback` });
return sendRedirect(event, `/api/discord/auth?${params}`);
});

0 comments on commit 1a658dd

Please sign in to comment.