Skip to content

Commit

Permalink
Use react router, fix Firefox test
Browse files Browse the repository at this point in the history
  • Loading branch information
apedroferreira committed Feb 8, 2024
1 parent e35837f commit 172d2c3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/toolpad-app/src/runtime/ToolpadApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1660,7 +1660,7 @@ export function ToolpadAppProvider({
(window as any).toggleDevtools = () => toggleDevtools();
}, [toggleDevtools]);

const authContext = useAuth({ dom, basename, signInPagePath: `${basename}/signin` });
const authContext = useAuth({ dom, basename, signInPagePath: '/signin' });

const appHost = useNonNullableContext(AppHostContext);
const showPreviewHeader = shouldShowPreviewHeader(appHost);
Expand Down
12 changes: 8 additions & 4 deletions packages/toolpad-app/src/runtime/useAuth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import * as appDom from '@mui/toolpad-core/appDom';
import { useNonNullableContext } from '@mui/toolpad-utils/react';
import { useLocation, useNavigate } from 'react-router-dom';
import { AppHostContext } from './AppHostContext';

const AUTH_API_PATH = '/api/auth';
Expand Down Expand Up @@ -52,10 +53,13 @@ export const AuthContext = React.createContext<AuthPayload>({
interface UseAuthInput {
dom: appDom.RenderTree;
basename: string;
signInPagePath?: string;
signInPagePath: string;
}

export function useAuth({ dom, basename, signInPagePath }: UseAuthInput): AuthPayload {
const location = useLocation();
const navigate = useNavigate();

const authProviders = React.useMemo(() => {
const app = appDom.getApp(dom);
const authProviderConfigs = app.attributes.authentication?.providers ?? [];
Expand Down Expand Up @@ -107,10 +111,10 @@ export function useAuth({ dom, basename, signInPagePath }: UseAuthInput): AuthPa
setSession(null);
setIsSigningOut(false);

if (!signInPagePath || window.location.pathname !== signInPagePath) {
window.location.href = `${basename}${AUTH_SIGNIN_PATH}`;
if (location.pathname !== signInPagePath) {
navigate(signInPagePath);
}
}, [basename, getCsrfToken, signInPagePath]);
}, [basename, getCsrfToken, location.pathname, navigate, signInPagePath]);

const getSession = React.useCallback(async () => {
setIsSigningIn(true);
Expand Down
2 changes: 2 additions & 0 deletions test/integration/auth/domain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const currentDirectory = url.fileURLToPath(new URL('.', import.meta.url));
test.use({
ignoreConsoleErrors: [
/Failed to load resource: the server responded with a status of 401 \(Unauthorized\)/,
/NetworkError when attempting to fetch resource./,
/The operation was aborted./,
],
});

Expand Down

0 comments on commit 172d2c3

Please sign in to comment.