mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(ui): use full page navigation for SSO login (#52213)
* fix(ui): use full page navigation for SSO login * style(ui): format SSO navigation regression test --------- Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
parent
ea96ea3db5
commit
55e4ec14ab
2 changed files with 40 additions and 28 deletions
|
|
@ -142,31 +142,40 @@ describe("LoginPage", () => {
|
|||
});
|
||||
});
|
||||
|
||||
it("should call router.push to SSO when jwt is invalid and auto_redirect_to_sso is true", async () => {
|
||||
const invalidToken = "invalid-token";
|
||||
(useUIConfig as ReturnType<typeof vi.fn>).mockReturnValue({
|
||||
data: {
|
||||
auto_redirect_to_sso: true,
|
||||
server_root_path: "/",
|
||||
proxy_base_url: null,
|
||||
sso_configured: true,
|
||||
},
|
||||
isLoading: false,
|
||||
});
|
||||
(getCookieFromDocument as ReturnType<typeof vi.fn>).mockReturnValue(invalidToken);
|
||||
(isJwtExpired as ReturnType<typeof vi.fn>).mockReturnValue(true);
|
||||
it.each([null, "/ui/?page=usage"])(
|
||||
"navigates the document to SSO automatically with return URL %s",
|
||||
async (returnUrl) => {
|
||||
if (returnUrl) {
|
||||
document.cookie = `litellm_return_url=${encodeURIComponent(returnUrl)}; Path=/`;
|
||||
}
|
||||
const invalidToken = "invalid-token";
|
||||
(useUIConfig as ReturnType<typeof vi.fn>).mockReturnValue({
|
||||
data: {
|
||||
auto_redirect_to_sso: true,
|
||||
server_root_path: "/",
|
||||
proxy_base_url: null,
|
||||
sso_configured: true,
|
||||
},
|
||||
isLoading: false,
|
||||
});
|
||||
(getCookieFromDocument as ReturnType<typeof vi.fn>).mockReturnValue(invalidToken);
|
||||
(isJwtExpired as ReturnType<typeof vi.fn>).mockReturnValue(true);
|
||||
|
||||
const queryClient = createQueryClient();
|
||||
render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<LoginPage />
|
||||
</QueryClientProvider>,
|
||||
);
|
||||
const queryClient = createQueryClient();
|
||||
render(
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<LoginPage />
|
||||
</QueryClientProvider>,
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockPush).toHaveBeenCalledWith("http://localhost:4000/sso/key/generate");
|
||||
});
|
||||
});
|
||||
await waitFor(() => {
|
||||
expect(mockAssignDocument).toHaveBeenCalledWith(
|
||||
`http://localhost:4000/sso/key/generate${returnUrl ? `?redirect_to=${encodeURIComponent(returnUrl)}` : ""}`,
|
||||
);
|
||||
});
|
||||
expect(mockPush).not.toHaveBeenCalled();
|
||||
},
|
||||
);
|
||||
|
||||
it("should not call router when jwt is invalid and auto_redirect_to_sso is false", async () => {
|
||||
const invalidToken = "invalid-token";
|
||||
|
|
@ -278,6 +287,11 @@ describe("LoginPage", () => {
|
|||
});
|
||||
|
||||
expect(screen.getByRole("button", { name: "Login with SSO" })).toBeInTheDocument();
|
||||
fireEvent.click(screen.getByRole("button", { name: "Login with SSO" }));
|
||||
expect(mockAssignDocument).toHaveBeenCalledWith(
|
||||
`http://localhost:4000/sso/key/generate?return_to=${encodeURIComponent(`${window.location.origin}/ui/login/`)}`,
|
||||
);
|
||||
expect(mockPush).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should show disabled Login with SSO button with popover when sso_configured is false", async () => {
|
||||
|
|
|
|||
|
|
@ -25,7 +25,6 @@ import { isJwtExpired } from "@/utils/jwtUtils";
|
|||
import { consumeReturnUrl, getLoginUrl, getReturnUrl, isValidReturnUrl } from "@/utils/returnUrlUtils";
|
||||
import { navigateToUiPage } from "@/utils/uiNavigation";
|
||||
import { CircleAlert, Info, TriangleAlert, X } from "lucide-react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect, useId, useState } from "react";
|
||||
import { Trans, useTranslation } from "react-i18next";
|
||||
import { z } from "zod";
|
||||
|
|
@ -80,7 +79,6 @@ function LoginPageContent() {
|
|||
const [isLoading, setIsLoading] = useState(true);
|
||||
const { data: uiConfig, isLoading: isConfigLoading } = useUIConfig();
|
||||
const loginMutation = useLogin();
|
||||
const router = useRouter();
|
||||
const { workers, selectWorker } = useWorker();
|
||||
const [selectedWorkerId, setSelectedWorkerId] = useState<string | null>(null);
|
||||
const workerFieldId = useId();
|
||||
|
|
@ -149,12 +147,12 @@ function LoginPageContent() {
|
|||
if (returnUrl && isValidReturnUrl(returnUrl)) {
|
||||
ssoUrl += `?redirect_to=${encodeURIComponent(returnUrl)}`;
|
||||
}
|
||||
router.push(ssoUrl);
|
||||
window.location.assign(ssoUrl);
|
||||
return;
|
||||
}
|
||||
|
||||
setIsLoading(false);
|
||||
}, [isConfigLoading, router, uiConfig]);
|
||||
}, [isConfigLoading, uiConfig]);
|
||||
|
||||
const handleSubmit = ({ username, password }: LoginFormValues) => {
|
||||
// If a worker is selected, point proxyBaseUrl at it before login
|
||||
|
|
@ -355,7 +353,7 @@ function LoginPageContent() {
|
|||
// include return_to so the callback redirects back here
|
||||
const ssoBase = selectedWorker?.url ?? getProxyBaseUrl();
|
||||
const returnTo = encodeURIComponent(getLoginUrl(window.location.origin));
|
||||
router.push(`${ssoBase}/sso/key/generate?return_to=${returnTo}`);
|
||||
window.location.assign(`${ssoBase}/sso/key/generate?return_to=${returnTo}`);
|
||||
}}
|
||||
className="w-full"
|
||||
>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue