Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/components/auth/AuthProviderWithRedirect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import { Auth0Provider } from "@auth0/auth0-react";
import type { FC, PropsWithChildren } from "react";
import { useNavigate } from "react-router-dom";

const domain = import.meta.env.VITE_AUTH_DOMAIN as string | undefined;
const clientId = import.meta.env.VITE_AUTH_CLIENT_ID as string | undefined;
const redirectUri = import.meta.env.VITE_AUTH_REDIRECT_URL as string | undefined;

if (!domain || !clientId || !redirectUri) {
throw new Error("Missing environment variables");
}
type Props = {
domain: string;
clientId: string;
redirectUri: string;
};

const Auth0ProviderWithRedirect: FC<PropsWithChildren<object>> = ({ children }) => {
const Auth0ProviderWithRedirect: FC<PropsWithChildren<Props>> = ({
children,
domain,
clientId,
redirectUri,
}) => {
const navigate = useNavigate();

const onRedirectCallback = async () => {
Expand Down
81 changes: 53 additions & 28 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ import LandingPage from "./pages/LandingPage.tsx";

const queryClient = new QueryClient();

const domain = import.meta.env.VITE_AUTH_DOMAIN as string | undefined;
const clientId = import.meta.env.VITE_AUTH_CLIENT_ID as string | undefined;
const redirectUri = import.meta.env.VITE_AUTH_REDIRECT_URL as string | undefined;

if (!domain || !clientId || !redirectUri) {
const missing = [
!domain && "VITE_AUTH_DOMAIN",
!clientId && "VITE_AUTH_CLIENT_ID",
!redirectUri && "VITE_AUTH_REDIRECT_URL",
]
.filter(Boolean)
.join(", ");
console.warn(
`Missing ${missing}. Auth0 is disabled for this dev session — routes requiring auth will not work. See .env.template.`,
);
}

const router = createBrowserRouter([
{
path: "/",
Expand All @@ -21,35 +38,43 @@ const router = createBrowserRouter([
</QueryClientProvider>
),
},
{
path: "/:envId",
element: (
<Auth0ProviderWithRedirect>
<QueryClientProvider client={queryClient}>
<ErrorBoundary
fallbackRender={({ error }) => (
<div>
There was an error!{" "}
<pre>{error instanceof Error ? error.message : String(error)}</pre>
</div>
)}
>
<Suspense
fallback={
<div className="flex w-screen h-screen justify-center">
<Loader />
</div>
}
...(domain && clientId && redirectUri
? [
{
path: "/:envId",
element: (
<Auth0ProviderWithRedirect
domain={domain}
clientId={clientId}
redirectUri={redirectUri}
>
<AppContextComponent>
<LandingPage />
</AppContextComponent>
</Suspense>
</ErrorBoundary>
</QueryClientProvider>
</Auth0ProviderWithRedirect>
),
},
<QueryClientProvider client={queryClient}>
<ErrorBoundary
fallbackRender={({ error }) => (
<div>
There was an error!{" "}
<pre>{error instanceof Error ? error.message : String(error)}</pre>
</div>
)}
>
<Suspense
fallback={
<div className="flex w-screen h-screen justify-center">
<Loader />
</div>
}
>
<AppContextComponent>
<LandingPage />
</AppContextComponent>
</Suspense>
</ErrorBoundary>
</QueryClientProvider>
</Auth0ProviderWithRedirect>
),
},
]
: []),
]);

const root = document.getElementById("root");
Expand Down
Loading