import { LoginForm } from "@/components/auth/login-form"; import { GenericIcon } from "@/components/icons/generic"; import { GithubIcon } from "@/components/icons/github"; import { GoogleIcon } from "@/components/icons/google"; import { Card, CardHeader, CardTitle, CardDescription, CardContent, } from "@/components/ui/card"; import { OAuthButton } from "@/components/ui/oauth-button"; import { SeperatorWithChildren } from "@/components/ui/separator"; import { useAppContext } from "@/context/app-context"; import { useUserContext } from "@/context/user-context"; import { useIsMounted } from "@/lib/hooks/use-is-mounted"; import { LoginSchema } from "@/schemas/login-schema"; import { useMutation } from "@tanstack/react-query"; import axios, { AxiosError } from "axios"; import { useEffect, useRef } from "react"; import { useTranslation } from "react-i18next"; import { Navigate, useLocation } from "react-router"; import { toast } from "sonner"; export const LoginPage = () => { const { isLoggedIn } = useUserContext(); const { configuredProviders, title, oauthAutoRedirect, genericName } = useAppContext(); const { search } = useLocation(); const { t } = useTranslation(); const isMounted = useIsMounted(); const redirectTimer = useRef(null); const searchParams = new URLSearchParams(search); const redirectUri = searchParams.get("redirect_uri"); const oauthConfigured = configuredProviders.filter((provider) => provider !== "username").length > 0; const userAuthConfigured = configuredProviders.includes("username"); const oauthMutation = useMutation({ mutationFn: (provider: string) => axios.get( `/api/oauth/url/${provider}?redirect_uri=${encodeURIComponent(redirectUri ?? "")}`, ), mutationKey: ["oauth"], onSuccess: (data) => { toast.info(t("loginOauthSuccessTitle"), { description: t("loginOauthSuccessSubtitle"), }); redirectTimer.current = window.setTimeout(() => { window.location.replace(data.data.url); }, 500); }, onError: () => { toast.error(t("loginOauthFailTitle"), { description: t("loginOauthFailSubtitle"), }); }, }); const loginMutation = useMutation({ mutationFn: (values: LoginSchema) => axios.post("/api/user/login", values), mutationKey: ["login"], onSuccess: (data) => { if (data.data.totpPending) { window.location.replace( `/totp?redirect_uri=${encodeURIComponent(redirectUri ?? "")}`, ); return; } toast.success(t("loginSuccessTitle"), { description: t("loginSuccessSubtitle"), }); redirectTimer.current = window.setTimeout(() => { window.location.replace( `/continue?redirect_uri=${encodeURIComponent(redirectUri ?? "")}`, ); }, 500); }, onError: (error: AxiosError) => { toast.error(t("loginFailTitle"), { description: error.response?.status === 429 ? t("loginFailRateLimit") : t("loginFailSubtitle"), }); }, }); useEffect(() => { if (isMounted()) { if ( oauthConfigured && configuredProviders.includes(oauthAutoRedirect) && !isLoggedIn && redirectUri ) { oauthMutation.mutate(oauthAutoRedirect); } } }, []); useEffect( () => () => { if (redirectTimer.current) clearTimeout(redirectTimer.current); }, [], ); if (isLoggedIn) { return ; } return ( {title} {configuredProviders.length > 0 && ( {oauthConfigured ? t("loginTitle") : t("loginTitleSimple")} )} {oauthConfigured && (
{configuredProviders.includes("google") && ( } className="w-full" onClick={() => oauthMutation.mutate("google")} loading={ oauthMutation.isPending && oauthMutation.variables === "google" } disabled={oauthMutation.isPending || loginMutation.isPending} /> )} {configuredProviders.includes("github") && ( } className="w-full" onClick={() => oauthMutation.mutate("github")} loading={ oauthMutation.isPending && oauthMutation.variables === "github" } disabled={oauthMutation.isPending || loginMutation.isPending} /> )} {configuredProviders.includes("generic") && ( } className="w-full" onClick={() => oauthMutation.mutate("generic")} loading={ oauthMutation.isPending && oauthMutation.variables === "generic" } disabled={oauthMutation.isPending || loginMutation.isPending} /> )}
)} {userAuthConfigured && oauthConfigured && ( {t("loginDivider")} )} {userAuthConfigured && ( loginMutation.mutate(values)} loading={loginMutation.isPending || oauthMutation.isPending} /> )} {configuredProviders.length == 0 && (

{t("failedToFetchProvidersTitle")}

)}
); };