diff --git a/src/frontend/apps/desk/src/api/__tests__/fetchApi.test.tsx b/src/frontend/apps/desk/src/api/__tests__/fetchApi.test.tsx index 6c15962..f1c1f62 100644 --- a/src/frontend/apps/desk/src/api/__tests__/fetchApi.test.tsx +++ b/src/frontend/apps/desk/src/api/__tests__/fetchApi.test.tsx @@ -1,6 +1,6 @@ import fetchMock from 'fetch-mock'; -import useAuthStore from '@/auth/useAuthStore'; +import { useAuthStore } from '@/features/'; import { fetchAPI } from '../fetchApi'; diff --git a/src/frontend/apps/desk/src/api/fetchApi.ts b/src/frontend/apps/desk/src/api/fetchApi.ts index 1f68820..e0f8a18 100644 --- a/src/frontend/apps/desk/src/api/fetchApi.ts +++ b/src/frontend/apps/desk/src/api/fetchApi.ts @@ -1,4 +1,4 @@ -import useAuthStore from '@/auth/useAuthStore'; +import { useAuthStore } from '@/features/'; export const fetchAPI = async (input: string, init?: RequestInit) => { const apiUrl = `${process.env.NEXT_PUBLIC_API_URL}${input}`; diff --git a/src/frontend/apps/desk/src/app/InnerLayout.tsx b/src/frontend/apps/desk/src/app/InnerLayout.tsx index 6a949ee..262f7aa 100644 --- a/src/frontend/apps/desk/src/app/InnerLayout.tsx +++ b/src/frontend/apps/desk/src/app/InnerLayout.tsx @@ -1,34 +1,11 @@ -import { Loader } from '@openfun/cunningham-react'; -import { useEffect } from 'react'; - -import useAuthStore from '@/auth/useAuthStore'; import { Box } from '@/components'; -import { HEADER_HEIGHT, Header } from '@/features/header'; -import { Menu } from '@/features/menu'; +import { HEADER_HEIGHT, Header, Menu } from '@/features/'; export default function InnerLayout({ children, }: { children: React.ReactNode; }) { - const { initAuth, authenticated, initialized } = useAuthStore(); - - useEffect(() => { - if (initialized) { - return; - } - - initAuth(); - }, [initAuth, initialized]); - - if (!authenticated) { - return ( - - - - ); - } - return (
diff --git a/src/frontend/apps/desk/src/app/layout.tsx b/src/frontend/apps/desk/src/app/layout.tsx index 3277fba..891a778 100644 --- a/src/frontend/apps/desk/src/app/layout.tsx +++ b/src/frontend/apps/desk/src/app/layout.tsx @@ -5,10 +5,10 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import { ReactQueryDevtools } from '@tanstack/react-query-devtools'; import { useCunninghamTheme } from '@/cunningham'; - +import { Auth } from '@/features/auth/Auth'; import '@/i18n/initI18n'; -import InnerLayout from './InnerLayout'; +import InnerLayout from './InnerLayout'; import './globals.css'; const queryClient = new QueryClient(); @@ -26,7 +26,9 @@ export default function RootLayout({ - {children} + + {children} + diff --git a/src/frontend/apps/desk/src/features/auth/Auth.tsx b/src/frontend/apps/desk/src/features/auth/Auth.tsx new file mode 100644 index 0000000..6c9fb2b --- /dev/null +++ b/src/frontend/apps/desk/src/features/auth/Auth.tsx @@ -0,0 +1,28 @@ +import { Loader } from '@openfun/cunningham-react'; +import { PropsWithChildren, useEffect } from 'react'; + +import { Box } from '@/components'; + +import { useAuthStore } from './useAuthStore'; + +export const Auth = ({ children }: PropsWithChildren) => { + const { initAuth, authenticated, initialized } = useAuthStore(); + + useEffect(() => { + if (initialized) { + return; + } + + initAuth(); + }, [initAuth, initialized]); + + if (!authenticated) { + return ( + + + + ); + } + + return children; +}; diff --git a/src/frontend/apps/desk/src/features/auth/index.ts b/src/frontend/apps/desk/src/features/auth/index.ts new file mode 100644 index 0000000..c66c026 --- /dev/null +++ b/src/frontend/apps/desk/src/features/auth/index.ts @@ -0,0 +1,2 @@ +export * from './Auth'; +export * from './useAuthStore'; diff --git a/src/frontend/apps/desk/src/auth/keycloak.ts b/src/frontend/apps/desk/src/features/auth/keycloak.ts similarity index 100% rename from src/frontend/apps/desk/src/auth/keycloak.ts rename to src/frontend/apps/desk/src/features/auth/keycloak.ts diff --git a/src/frontend/apps/desk/src/auth/useAuthStore.tsx b/src/frontend/apps/desk/src/features/auth/useAuthStore.tsx similarity index 90% rename from src/frontend/apps/desk/src/auth/useAuthStore.tsx rename to src/frontend/apps/desk/src/features/auth/useAuthStore.tsx index f673cff..3b56929 100644 --- a/src/frontend/apps/desk/src/auth/useAuthStore.tsx +++ b/src/frontend/apps/desk/src/features/auth/useAuthStore.tsx @@ -16,7 +16,7 @@ const initialState = { token: null, }; -const useAuthStore = create((set) => ({ +export const useAuthStore = create((set) => ({ authenticated: initialState.authenticated, initialized: initialState.initialized, token: initialState.token, @@ -39,5 +39,3 @@ const useAuthStore = create((set) => ({ set(initialState); }, })); - -export default useAuthStore; diff --git a/src/frontend/apps/desk/src/features/index.ts b/src/frontend/apps/desk/src/features/index.ts index 5058260..08b8426 100644 --- a/src/frontend/apps/desk/src/features/index.ts +++ b/src/frontend/apps/desk/src/features/index.ts @@ -1,3 +1,4 @@ +export * from './auth/'; export * from './header/'; export * from './language/'; export * from './menu/';