Skip to content

Commit b32b168

Browse files
authored
Merge pull request #18 from kinde-oss/fix/redirect-on-login
Setting dynamic post login redirect url
2 parents a5877bb + 680fde5 commit b32b168

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/lib/config/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const kindeConfiguration = {
88
clientId: env.KINDE_CLIENT_ID,
99
logoutRedirectURL: env.KINDE_POST_LOGOUT_REDIRECT_URL,
1010
redirectURL: env.KINDE_REDIRECT_URL,
11+
appBase: new URL(env.KINDE_REDIRECT_URL).origin,
1112
audience: env.KINDE_AUDIENCE,
1213
scope: env.KINDE_SCOPE,
1314
clientSecret: env.KINDE_CLIENT_SECRET,

src/lib/handleAuth/handleAuth.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import {kindeAuthClient} from '$lib/KindeSDK.js';
22
import {kindeConfiguration} from '$lib/index.js';
3+
import {sessionStorage} from '$lib/sessionStorage/sessionStorage.js';
34
import {parseSearchParamsToObject} from '$lib/utils/index.js';
45
import type {SessionManager} from '@kinde-oss/kinde-typescript-sdk';
56
import {error, redirect, type RequestEvent} from '@sveltejs/kit';
67
import {version} from '$app/environment';
78

9+
const KEY_POST_LOGIN_REDIRECT_URL = 'post-login-redirect-url';
10+
811
export async function handleAuth({
912
request,
1013
params,
@@ -14,6 +17,7 @@ export async function handleAuth({
1417
let url: URL | null = null;
1518
switch (params.kindeAuth) {
1619
case 'login':
20+
storePostLoginRedirectUrl(options);
1721
url = await kindeAuthClient.login(request as unknown as SessionManager, options);
1822
break;
1923
case 'health':
@@ -43,6 +47,7 @@ export async function handleAuth({
4347
}
4448
);
4549
case 'register':
50+
storePostLoginRedirectUrl(options);
4651
url = await kindeAuthClient.register(request as unknown as SessionManager, options);
4752
break;
4853
case 'create_org':
@@ -53,6 +58,7 @@ export async function handleAuth({
5358
request as unknown as SessionManager,
5459
new URL(request.url)
5560
);
61+
redirectToPostLoginUrl();
5662
throw redirect(302, kindeConfiguration.loginRedirectURL ?? '/');
5763
case 'logout':
5864
url = await kindeAuthClient.logout(request as unknown as SessionManager);
@@ -62,3 +68,25 @@ export async function handleAuth({
6268
}
6369
throw redirect(302, url.toString());
6470
}
71+
72+
const storePostLoginRedirectUrl = (options: Record<string, string | number>) => {
73+
if (options.post_login_redirect_url && typeof options.post_login_redirect_url == 'string') {
74+
sessionStorage.setSessionItem(KEY_POST_LOGIN_REDIRECT_URL, options.post_login_redirect_url);
75+
}
76+
};
77+
78+
const isAbsoluteUrl = (url: string) =>
79+
url.indexOf('http://') === 0 || url.indexOf('https://') === 0;
80+
81+
const redirectToPostLoginUrl = () => {
82+
if (sessionStorage.getSessionItem(KEY_POST_LOGIN_REDIRECT_URL)) {
83+
const post_login_redirect_url = sessionStorage.getSessionItem(KEY_POST_LOGIN_REDIRECT_URL);
84+
sessionStorage.removeSessionItem(KEY_POST_LOGIN_REDIRECT_URL);
85+
86+
if (isAbsoluteUrl(post_login_redirect_url)) {
87+
throw redirect(302, new URL(post_login_redirect_url));
88+
} else {
89+
throw redirect(302, new URL(post_login_redirect_url, kindeConfiguration.appBase));
90+
}
91+
}
92+
};

0 commit comments

Comments
 (0)