#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
import React, { useMemo } from "react";
import { Navigate, useLocation } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { useAuth } from "@editor/auth/UseSession";
import { useAutoAnonymousAuth } from "@editor/hooks/useAutoAnonymousAuth";
import { isHomeRoute, isToolRoute } from "@editor/utils/pathUtils";
import { AppRoot } from "@editor/components/layout/AppRoot";
import Login from "@editor/routes/Login";
import GuestUserBanner from "@editor/components/auth/GuestUserBanner";
import { TeamInvitationBanner } from "@editor/components/shared/TeamInvitationBanner";
export default function Landing() {
const { t } = useTranslation();
const { session, loading } = useAuth();
const { isAutoAuthenticating, autoAuthError, shouldTriggerAutoAuth } =
useAutoAnonymousAuth();
const location = useLocation();
// Check if current path is a tool (prevents premature navigation on first render)
const isCurrentPathTool = useMemo(
() => isToolRoute(location.pathname),
[location.pathname],
);
// Match the same guarded bypass used in RequireAuth
const isLocalhost =
typeof window !== "undefined" &&
/^(localhost|127\.0\.0\.1)$/i.test(window.location.hostname);
const devBypassEnabled = Boolean(
import.meta.env.DEV &&
isLocalhost &&
import.meta.env.VITE_DEV_BYPASS_AUTH === "true",
);
console.log("[Landing] State:", {
pathname: location.pathname,
loading,
hasSession: !!session,
isAutoAuthenticating,
shouldTriggerAutoAuth,
isCurrentPathTool,
autoAuthError,
});
// Show loading while checking auth, while auto-authenticating, OR while preparing to auto-authenticate
// CRITICAL: Also wait if shouldTriggerAutoAuth is true OR if we're on a tool route (prevents navigation before hook evaluates)
if (
loading ||
isAutoAuthenticating ||
(!session && (shouldTriggerAutoAuth || isCurrentPathTool) && !autoAuthError)
) {
return (
<div
style={{
minHeight: "100vh",
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<div className="text-center">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600 mx-auto mb-3"></div>
<div className="text-gray-600">
{isAutoAuthenticating
? t("landing.settingUpSession", "Setting up your session...")
: t("loading", "Loading...")}
</div>
</div>
</div>
);
}
// If we have a session or dev bypass is enabled, show the main app
if (session || devBypassEnabled) {
return (
<>
<GuestUserBanner />
<TeamInvitationBanner />
<AppRoot />
</>
);
}
// Where to come back to after signing in. Carried in the URL (not just router
// state) so it survives a reload, and because `next` is what Login forwards
// through the OAuth round-trip. Landing on /editor signed out therefore
// returns to /editor after login rather than to the role router.
const returnTo = encodeURIComponent(location.pathname + location.search);
// If auto-authentication failed, navigate to login with error state
if (autoAuthError && shouldTriggerAutoAuth) {
return (
<Navigate
to={`/login?next=${returnTo}`}
replace
state={{ autoAuthError, from: location }}
/>
);
}
// If we're at home route ("/"), show login directly (marketing/landing page)
// Otherwise navigate to login (fixes URL mismatch for tool routes)
if (location.pathname === "" || isHomeRoute(location.pathname)) {
return <Login />;
}
// For non-home routes without auth, navigate to login (preserves where we came from)
return (
<Navigate
to={`/login?next=${returnTo}`}
replace
state={{ from: location }}
/>
);
}