Skip to content

Commit

Permalink
PMM-11231 Rework redirect to login
Browse files Browse the repository at this point in the history
  • Loading branch information
matejkubinec committed Jul 4, 2024
1 parent e9a654f commit a46f06f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
28 changes: 15 additions & 13 deletions ui/src/components/main/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ import { useAuth } from 'contexts/auth';
export const Main = () => {
const { isLoading } = useAuth();

if (isLoading) {
return (
<Stack
alignItems="center"
justifyContent="center"
sx={{
padding: 10,
}}
>
<CircularProgress data-testid="pmm-loading-indicator" />
</Stack>
);
}

return (
<Stack>
<AppBar />
{isLoading ? (
<Stack
alignItems="center"
justifyContent="center"
sx={{
padding: 10,
}}
>
<CircularProgress data-testid="pmm-loading-indicator" />
</Stack>
) : (
<Outlet />
)}
<Outlet />
</Stack>
);
};
1 change: 1 addition & 0 deletions ui/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const PMM_HOME_URL = '/graph/d/pmm-home';
export const PMM_LOGIN_URL = '/graph/login';
15 changes: 8 additions & 7 deletions ui/src/contexts/auth/auth.provider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, PropsWithChildren, useEffect } from 'react';
import { FC, PropsWithChildren, useMemo } from 'react';
import { AuthContext } from './auth.context';
import { useQuery } from '@tanstack/react-query';
import { rotateToken } from 'api/auth';
Expand All @@ -13,15 +13,16 @@ export const AuthProvider: FC<PropsWithChildren> = ({ children }) => {
refetchIntervalInBackground: true,
retry: false,
});

useEffect(() => {
const shouldRedirectToLogin = useMemo(() => {
const response = (error as AxiosError)?.response;

if (response?.status === HttpStatusCode.Unauthorized) {
redirectToLogin();
}
return response?.status === HttpStatusCode.Unauthorized;
}, [error]);

if (shouldRedirectToLogin) {
redirectToLogin();
return null;
}

return (
<AuthContext.Provider value={{ isLoading }}>
{children}
Expand Down
4 changes: 3 additions & 1 deletion ui/src/contexts/auth/auth.utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { PMM_LOGIN_URL } from 'constants';

export const getSessionExpiry = () => {
const expiryCookie = getSessionExpiryCookie();

Expand Down Expand Up @@ -33,5 +35,5 @@ export const getSessionExpiryCookie = () =>
.find((row) => row.startsWith('grafana_session_expiry='));

export const redirectToLogin = () => {
window.location.replace('/graph/login');
window.location.replace(PMM_LOGIN_URL);
};

0 comments on commit a46f06f

Please sign in to comment.