fix session handling ui

This commit is contained in:
Ishaan Jaff 2025-02-28 13:30:01 -08:00
parent 5670a9f8b7
commit 3bc577bf12
5 changed files with 36 additions and 5 deletions

View file

@ -18,6 +18,7 @@ import {
getOnboardingCredentials,
claimOnboardingToken,
} from "@/components/networking";
import { setTokenCookie } from "@/utils/cookies";
import { jwtDecode } from "jwt-decode";
import { Form, Button as Button2, message } from "antd";
@ -95,7 +96,7 @@ export default function Onboarding() {
litellm_dashboard_ui += "?userID=" + user_id;
// set cookie "token" to jwtToken
document.cookie = "token=" + jwtToken;
setTokenCookie(jwtToken);
console.log("redirecting to:", litellm_dashboard_ui);
window.location.href = litellm_dashboard_ui;

View file

@ -4,6 +4,7 @@ import type { MenuProps } from "antd";
import { Dropdown } from "antd";
import { Organization } from "@/components/networking";
import { defaultOrg } from "@/components/common_components/default_org";
import { setTokenCookie } from "@/utils/cookies";
import {
UserOutlined,
LogoutOutlined
@ -27,7 +28,7 @@ const Navbar: React.FC<NavbarProps> = ({
let logoutUrl = proxySettings?.PROXY_LOGOUT_URL || "";
const handleLogout = () => {
document.cookie = "token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
setTokenCookie("")
window.location.href = logoutUrl;
};

View file

@ -2,7 +2,7 @@
* Helper file for calls being made to proxy
*/
import { message } from "antd";
import { setTokenCookie } from "@/utils/cookies";
const isLocal = process.env.NODE_ENV === "development";
export const proxyBaseUrl = isLocal ? "http://localhost:4000" : null;
if (isLocal != true) {
@ -50,7 +50,7 @@ const handleError = async (errorData: string) => {
message.info("UI Session Expired. Logging out.");
lastErrorTime = currentTime;
await sleep(3000); // 5 second sleep
document.cookie = "token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
setTokenCookie("");
window.location.href = baseUrl;
}
lastErrorTime = currentTime;

View file

@ -18,6 +18,7 @@ import ViewUserTeam from "./view_user_team";
import DashboardTeam from "./dashboard_default_team";
import Onboarding from "../app/onboarding/page";
import { useSearchParams, useRouter } from "next/navigation";
import { setTokenCookie } from "@/utils/cookies";
import { Team } from "./key_team_helpers/key_list";
import { jwtDecode } from "jwt-decode";
import { Typography } from "antd";
@ -301,7 +302,7 @@ const UserDashboard: React.FC<UserDashboardProps> = ({
// clear cookie called "token" since user will be logging in again
document.cookie = "token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
setTokenCookie("");
console.log("Full URL:", url);
window.location.href = url;

View file

@ -0,0 +1,28 @@
/**
* Utility functions for cookie management
*/
/**
* Get a cookie value by name
* @param name The name of the cookie to retrieve
* @returns The cookie value or null if not found
*/
export function getCookie(name: string): string | null {
const cookieValue = document.cookie
.split('; ')
.find(row => row.startsWith(name + '='));
return cookieValue ? cookieValue.split('=')[1] : null;
}
/**
* Set a token cookie, removing any existing ones first
* @param value The value to set for the token
* @param options Additional cookie options
*/
export function setTokenCookie(value: string): void {
// Delete existing token cookie by setting expiration in the past
document.cookie = "token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
// Set new token cookie with proper attributes
document.cookie = `token=${value}; path=/; max-age=2592000; SameSite=Strict`;
}