1- import { Loader } from '@openfun/cunningham-react' ;
21import { useRouter } from 'next/router' ;
3- import { PropsWithChildren } from 'react' ;
2+ import { PropsWithChildren , useEffect , useState } from 'react' ;
43
5- import { Box } from '@/components' ;
4+ import { Loading } from '@/components' ;
65import { useConfig } from '@/core' ;
76
87import { HOME_URL } from '../conf' ;
@@ -14,57 +13,65 @@ export const Auth = ({ children }: PropsWithChildren) => {
1413 useAuth ( ) ;
1514 const { replace, pathname } = useRouter ( ) ;
1615 const { data : config } = useConfig ( ) ;
17-
18- if ( isLoading && ! isFetchedAfterMount ) {
19- return (
20- < Box $height = "100vh" $width = "100vw" $align = "center" $justify = "center" >
21- < Loader />
22- </ Box >
23- ) ;
24- }
16+ const [ isRedirecting , setIsRedirecting ] = useState ( false ) ;
2517
2618 /**
27- * If the user is authenticated and wanted initially to access a document,
28- * we redirect to the document page.
19+ * If the user is authenticated and initially wanted to access a specific page, redirect him to that page now.
2920 */
30- if ( authenticated ) {
21+ useEffect ( ( ) => {
22+ if ( ! authenticated || isRedirecting ) {
23+ return ;
24+ }
25+
3126 const authUrl = getAuthUrl ( ) ;
3227 if ( authUrl ) {
33- void replace ( authUrl ) ;
34- return (
35- < Box $height = "100vh" $width = "100vw" $align = "center" $justify = "center" >
36- < Loader />
37- </ Box >
38- ) ;
28+ setIsRedirecting ( true ) ;
29+ void replace ( authUrl ) . then ( ( ) => setIsRedirecting ( false ) ) ;
3930 }
40- }
31+ } , [ authenticated , isRedirecting , pathname , replace ] ) ;
4132
4233 /**
43- * If the user is not authenticated and the path is not allowed, we redirect to the login page.
34+ * If the user is not authenticated and not on a allowed pages
4435 */
45- if ( ! authenticated && ! pathAllowed ) {
36+ useEffect ( ( ) => {
37+ if ( isLoading || authenticated || pathAllowed || isRedirecting ) {
38+ return ;
39+ }
40+
41+ /**
42+ * The homepage feature is enabled, redirect them to the homepage
43+ */
4644 if ( config ?. FRONTEND_HOMEPAGE_FEATURE_ENABLED ) {
47- void replace ( HOME_URL ) ;
48- } else {
49- gotoLogin ( ) ;
45+ if ( pathname !== HOME_URL ) {
46+ setIsRedirecting ( true ) ;
47+ void replace ( HOME_URL ) . then ( ( ) => setIsRedirecting ( false ) ) ;
48+ }
49+
50+ return ;
5051 }
51- return (
52- < Box $height = "100vh" $width = "100vw" $align = "center" $justify = "center" >
53- < Loader />
54- </ Box >
55- ) ;
56- }
5752
58- /**
59- * If the user is authenticated and the path is the home page, we redirect to the index.
60- */
61- if ( pathname === HOME_URL && authenticated ) {
62- void replace ( '/' ) ;
63- return (
64- < Box $height = "100vh" $width = "100vw" $align = "center" $justify = "center" >
65- < Loader />
66- </ Box >
67- ) ;
53+ /**
54+ * Redirect them to login page
55+ */
56+ setIsRedirecting ( true ) ;
57+ gotoLogin ( ) ;
58+ } , [
59+ authenticated ,
60+ pathAllowed ,
61+ config ?. FRONTEND_HOMEPAGE_FEATURE_ENABLED ,
62+ replace ,
63+ isLoading ,
64+ isRedirecting ,
65+ pathname ,
66+ ] ) ;
67+
68+ const shouldShowLoader =
69+ ( isLoading && ! isFetchedAfterMount ) ||
70+ isRedirecting ||
71+ ( ! authenticated && ! pathAllowed ) ;
72+
73+ if ( shouldShowLoader ) {
74+ return < Loading $height = "100vh" $width = "100vw" /> ;
6875 }
6976
7077 return children ;
0 commit comments