Skip to content

Commit 45f9b91

Browse files
authored
Merge pull request #87 from pesickaa/fix/unhandled-promise-rejection
fix: ensure we await the redirectToPostLoginUrl as it is a promise
2 parents 829052e + 3409a70 commit 45f9b91

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/lib/handleAuth/handleAuth.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ export async function handleAuth({
8282
request as unknown as SessionManager,
8383
new URL(request.url),
8484
);
85-
redirectToPostLoginUrl();
86-
return redirect(302, kindeConfiguration.loginRedirectURL ?? "/");
85+
await redirectToPostLoginUrl();
86+
throw redirect(302, kindeConfiguration.loginRedirectURL || "/");
8787
case "logout":
8888
url = await kindeAuthClient.logout(request as unknown as SessionManager);
8989
break;
9090
default:
9191
return error(404, "Not Found");
9292
}
93-
redirect(302, url.toString());
93+
throw redirect(302, url.toString());
9494
}
9595

9696
const openPortal = async (
@@ -115,7 +115,7 @@ const openPortal = async (
115115
console.log("err:", err);
116116
throw error(500, "Failed to generate portal URL");
117117
}
118-
redirect(302, portalUrl.url.toString());
118+
throw redirect(302, portalUrl.url.toString());
119119
};
120120

121121
const storePostLoginRedirectUrl = (
@@ -136,19 +136,22 @@ const isAbsoluteUrl = (url: string) =>
136136
url.indexOf("http://") === 0 || url.indexOf("https://") === 0;
137137

138138
const redirectToPostLoginUrl = async () => {
139-
if (await sessionStorage.getSessionItem(KEY_POST_LOGIN_REDIRECT_URL)) {
140-
const post_login_redirect_url = (await sessionStorage.getSessionItem(
141-
KEY_POST_LOGIN_REDIRECT_URL,
142-
)) as string;
143-
sessionStorage.removeSessionItem(KEY_POST_LOGIN_REDIRECT_URL);
139+
const value = await sessionStorage.getSessionItem(
140+
KEY_POST_LOGIN_REDIRECT_URL,
141+
);
142+
if (!value || typeof value !== "string") {
143+
return;
144+
}
145+
const post_login_redirect_url = value as string;
146+
sessionStorage.removeSessionItem(KEY_POST_LOGIN_REDIRECT_URL);
144147

145-
if (isAbsoluteUrl(post_login_redirect_url)) {
146-
redirect(302, new URL(post_login_redirect_url));
147-
} else {
148-
redirect(
149-
302,
150-
new URL(post_login_redirect_url, kindeConfiguration.appBase),
151-
);
148+
const appBaseUrl = new URL(kindeConfiguration.appBase);
149+
if (isAbsoluteUrl(post_login_redirect_url)) {
150+
const target = new URL(post_login_redirect_url);
151+
if (target.origin !== appBaseUrl.origin) {
152+
return;
152153
}
154+
throw redirect(302, target.toString());
153155
}
156+
throw redirect(302, new URL(post_login_redirect_url, appBaseUrl).toString());
154157
};

0 commit comments

Comments
 (0)