Skip to content

Fix infinite flicker in firefox #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025
Merged
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
40 changes: 36 additions & 4 deletions react/src/components/ProtectedApp.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
import { useQuery } from '@tanstack/react-query';
import { type ReactNode, useEffect, useState } from 'react';
import { hasAuthParams, useAuth } from 'react-oidc-context';
import { Alert } from './Alert.tsx';

const getMetadata = async (metadataUrl?: string) => {
if (!metadataUrl) {
throw new Error('metadataUrl is required');
}
let response: Response;
try {
response = await fetch(metadataUrl, {
mode: 'no-cors',
headers: { accept: 'application/jwk-set+json, application/json' }
});
} catch {
throw new Error(`Unable to fetch metadataUrl\n\n${metadataUrl}\n\nPlease confirm your auth server is up`);
}
if (!response.ok) {
throw new Error(`Unexpected response status: ${response.status}`);
}
return await response.json();
};

interface ProtectedAppProps {
children: ReactNode;
}

export const ProtectedApp: React.FC<ProtectedAppProps> = (props) => {
const { children } = props;

const { isPending: metadataIsPending, error: metadataError } = useQuery({
queryKey: ['getMetadata'],
retry: false,
queryFn: () => getMetadata(auth.settings.metadataUrl)
});

const auth = useAuth();
const [hasTriedSignin, setHasTriedSignin] = useState(false);

Expand All @@ -18,20 +44,26 @@ export const ProtectedApp: React.FC<ProtectedAppProps> = (props) => {
* See {@link https://github.com/authts/react-oidc-context?tab=readme-ov-file#automatic-sign-in}
*/
useEffect(() => {
if (metadataIsPending || metadataError) {
return;
}
if (!(hasAuthParams() || auth.isAuthenticated || auth.activeNavigator || auth.isLoading || hasTriedSignin)) {
void auth.signinRedirect();
setHasTriedSignin(true);
}
}, [auth, hasTriedSignin]);
}, [auth, hasTriedSignin, metadataIsPending, metadataError]);

const anyLoading = auth.isLoading || metadataIsPending;
const anyErrorMessage = auth.error?.message || metadataError?.message;

return (
<>
{auth.error ? (
{anyErrorMessage ? (
<>
<h1>We've hit a snag</h1>
<Alert variant="error">{auth.error?.message}</Alert>
<Alert variant="error">{anyErrorMessage}</Alert>
</>
) : auth.isLoading ? (
) : anyLoading ? (
<>
<h1>Loading...</h1>
</>
Expand Down