Skip to content

Commit 40dc5fe

Browse files
Fix login flows (#486)
* ad usage and * allow API Key access inactive models * fix the login flows
1 parent ca9323d commit 40dc5fe

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

apps/web/src/components/form/login.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,25 @@ export function LoginForm({
3838
const getRedirectUrl = () => {
3939
const url = new URL(window.location.href);
4040
const redirectParam = url.searchParams.get(URL_PARAM.REDIRECT);
41+
42+
// Case 1: Has redirect param (external localhost or internal path)
4143
if (redirectParam && (redirectParam.startsWith("/") || isAllowedExternalRedirect(redirectParam))) {
4244
return redirectParam;
4345
}
46+
47+
// Case 2: On /login route without redirect -> go to homepage
4448
if (url.pathname === "/login") {
45-
return "/";
49+
return undefined; // Let handleCloseModal decide
4650
}
51+
52+
// Case 3: Modal login (/?modal=login) -> return undefined to close modal and stay/go home
53+
if (url.searchParams.get(URL_PARAM.MODAL) === URL_PARAM_VALUE.LOGIN) {
54+
return undefined;
55+
}
56+
57+
// Case 4: Modal opened on another page -> stay on current page (without modal params)
58+
url.searchParams.delete(URL_PARAM.MODAL);
59+
url.searchParams.delete(URL_PARAM.REDIRECT);
4760
return url.pathname + url.search;
4861
};
4962

apps/web/src/routes/__root.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,27 +142,30 @@ function RootLayout() {
142142
};
143143

144144
const handleCloseModal = (redirectUrl?: string) => {
145-
// Handle external redirect (e.g., http://localhost:19999)
145+
// Case 1: External redirect (e.g., http://localhost:29999 from install script)
146146
if (redirectUrl && isAllowedExternalRedirect(redirectUrl)) {
147+
const bearerToken = `Bearer ${accessToken}`;
148+
const encodedToken = btoa(bearerToken);
147149
const target = new URL(redirectUrl);
148-
target.searchParams.set("token", `Bearer ${accessToken}`);
150+
target.searchParams.set("token", encodedToken);
149151
window.location.href = target.toString();
150152
return;
151153
}
152154

153-
if (location.pathname === "/login") {
154-
if (redirectUrl && redirectUrl.startsWith("/")) {
155-
router.navigate({ to: redirectUrl });
156-
}
155+
// Case 2: Internal redirect path (e.g., /dashboard)
156+
if (redirectUrl && redirectUrl.startsWith("/")) {
157+
router.navigate({ to: redirectUrl });
157158
return;
158159
}
159160

160-
if (redirectUrl && redirectUrl.startsWith("/")) {
161-
router.navigate({ to: redirectUrl });
161+
// Case 3: Modal login (/?modal=login) - close modal and go to homepage
162+
// Or /login route without redirect - go to homepage
163+
if (location.pathname === "/login") {
164+
router.navigate({ to: "/" });
162165
return;
163166
}
164167

165-
// Remove the modal search param by navigating without it
168+
// Case 4: Modal was opened on another page - just close modal, stay on current page
166169
const url = new URL(window.location.href);
167170
url.searchParams.delete(URL_PARAM.MODAL);
168171
url.searchParams.delete(URL_PARAM.REDIRECT);

0 commit comments

Comments
 (0)