From fe5654fd1e083074c47f450d0fe4d8c79a7a7ab0 Mon Sep 17 00:00:00 2001 From: pablodanswer Date: Wed, 16 Oct 2024 14:55:33 -0700 Subject: [PATCH] nit: naming --- web/src/components/chat_search/Header.tsx | 4 +- .../{Notification.tsx => Notifications.tsx} | 2 +- web/src/lib/userSS.ts | 166 ------------------ 3 files changed, 3 insertions(+), 169 deletions(-) rename web/src/components/chat_search/{Notification.tsx => Notifications.tsx} (99%) delete mode 100644 web/src/lib/userSS.ts diff --git a/web/src/components/chat_search/Header.tsx b/web/src/components/chat_search/Header.tsx index fc17b1d9d..760384b36 100644 --- a/web/src/components/chat_search/Header.tsx +++ b/web/src/components/chat_search/Header.tsx @@ -11,7 +11,7 @@ import { pageType } from "@/app/chat/sessionSidebar/types"; import { useRouter } from "next/navigation"; import { ChatBanner } from "@/app/chat/ChatBanner"; import LogoType from "../header/LogoType"; -import { NotificationCard } from "./Notification"; +import { Notifications } from "./Notifications"; import { useUser } from "../user/UserProvider"; export default function FunctionalHeader({ @@ -110,7 +110,7 @@ export default function FunctionalHeader({ )} - +
diff --git a/web/src/components/chat_search/Notification.tsx b/web/src/components/chat_search/Notifications.tsx similarity index 99% rename from web/src/components/chat_search/Notification.tsx rename to web/src/components/chat_search/Notifications.tsx index 6e896800d..c9c999d40 100644 --- a/web/src/components/chat_search/Notification.tsx +++ b/web/src/components/chat_search/Notifications.tsx @@ -12,7 +12,7 @@ import { useAssistants } from "../context/AssisantsContext"; import { useUser } from "../user/UserProvider"; import { Bell } from "@phosphor-icons/react"; -export const NotificationCard = () => { +export const Notifications = () => { const [showDropdown, setShowDropdown] = useState(false); const { data: notifications, diff --git a/web/src/lib/userSS.ts b/web/src/lib/userSS.ts deleted file mode 100644 index d0272b85b..000000000 --- a/web/src/lib/userSS.ts +++ /dev/null @@ -1,166 +0,0 @@ -import { cookies } from "next/headers"; -import { User } from "./types"; -import { buildUrl } from "./utilsSS"; -import { ReadonlyRequestCookies } from "next/dist/server/web/spec-extension/adapters/request-cookies"; -import { AuthType } from "./constants"; - -export interface AuthTypeMetadata { - authType: AuthType; - autoRedirect: boolean; - requiresVerification: boolean; -} - -export const getAuthTypeMetadataSS = async (): Promise => { - const res = await fetch(buildUrl("/auth/type")); - if (!res.ok) { - throw new Error("Failed to fetch data"); - } - - const data: { auth_type: string; requires_verification: boolean } = - await res.json(); - const authType = data.auth_type as AuthType; - - // for SAML / OIDC, we auto-redirect the user to the IdP when the user visits - // Danswer in an un-authenticated state - if (authType === "oidc" || authType === "saml") { - return { - authType, - autoRedirect: true, - requiresVerification: data.requires_verification, - }; - } - return { - authType, - autoRedirect: false, - requiresVerification: data.requires_verification, - }; -}; - -export const getAuthDisabledSS = async (): Promise => { - return (await getAuthTypeMetadataSS()).authType === "disabled"; -}; - -const getOIDCAuthUrlSS = async (nextUrl: string | null): Promise => { - const res = await fetch( - buildUrl( - `/auth/oidc/authorize${nextUrl ? `?next=${encodeURIComponent(nextUrl)}` : ""}` - ) - ); - if (!res.ok) { - throw new Error("Failed to fetch data"); - } - - const data: { authorization_url: string } = await res.json(); - return data.authorization_url; -}; - -const getGoogleOAuthUrlSS = async (): Promise => { - const res = await fetch(buildUrl(`/auth/oauth/authorize`)); - if (!res.ok) { - throw new Error("Failed to fetch data"); - } - - const data: { authorization_url: string } = await res.json(); - return data.authorization_url; -}; - -const getSAMLAuthUrlSS = async (): Promise => { - const res = await fetch(buildUrl("/auth/saml/authorize")); - if (!res.ok) { - throw new Error("Failed to fetch data"); - } - - const data: { authorization_url: string } = await res.json(); - return data.authorization_url; -}; - -export const getAuthUrlSS = async ( - authType: AuthType, - nextUrl: string | null -): Promise => { - // Returns the auth url for the given auth type - switch (authType) { - case "disabled": - return ""; - case "basic": - return ""; - case "google_oauth": { - return await getGoogleOAuthUrlSS(); - } - case "saml": { - return await getSAMLAuthUrlSS(); - } - case "oidc": { - return await getOIDCAuthUrlSS(nextUrl); - } - } -}; - -const logoutStandardSS = async (headers: Headers): Promise => { - return await fetch(buildUrl("/auth/logout"), { - method: "POST", - headers: headers, - }); -}; - -const logoutSAMLSS = async (headers: Headers): Promise => { - return await fetch(buildUrl("/auth/saml/logout"), { - method: "POST", - headers: headers, - }); -}; - -export const logoutSS = async ( - authType: AuthType, - headers: Headers -): Promise => { - switch (authType) { - case "disabled": - return null; - case "saml": { - return await logoutSAMLSS(headers); - } - default: { - return await logoutStandardSS(headers); - } - } -}; - -export const getCurrentUserSS = async (): Promise => { - try { - const response = await fetch(buildUrl("/me"), { - credentials: "include", - next: { revalidate: 0 }, - headers: { - cookie: cookies() - .getAll() - .map((cookie) => `${cookie.name}=${cookie.value}`) - .join("; "), - }, - }); - if (!response.ok) { - return null; - } - const user = await response.json(); - return user; - } catch (e) { - console.log(`Error fetching user: ${e}`); - return null; - } -}; - -export const processCookies = (cookies: ReadonlyRequestCookies): string => { - return cookies - .getAll() - .map((cookie) => `${cookie.name}=${cookie.value}`) - .join("; "); -}; - -export const getNotificationsSS = async (): Promise => { - const response = await fetch(buildUrl("/notifications")); - if (!response.ok) { - throw new Error("Failed to fetch notifications"); - } - const notifications = await response.json(); - return notifications; -};