@vtex/faststore-plugin-buyer-portal 1.1.114 → 1.1.115
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -12,6 +12,7 @@ import { AUT_COOKIE_KEY } from "../utils/constants";
|
|
|
12
12
|
*/
|
|
13
13
|
export const useCookieMonitor = () => {
|
|
14
14
|
const [hasCookie, setHasCookie] = useState(false);
|
|
15
|
+
const [timedOut, setTimedOut] = useState(false);
|
|
15
16
|
|
|
16
17
|
useEffect(() => {
|
|
17
18
|
const checkCookie = () => {
|
|
@@ -58,7 +59,10 @@ export const useCookieMonitor = () => {
|
|
|
58
59
|
|
|
59
60
|
// Cleanup after 5 seconds max
|
|
60
61
|
const timeout = setTimeout(() => {
|
|
61
|
-
console.log(
|
|
62
|
+
console.log(
|
|
63
|
+
"[useCookieMonitor] Polling timeout reached (5s) - cookie never appeared"
|
|
64
|
+
);
|
|
65
|
+
setTimedOut(true);
|
|
62
66
|
clearInterval(interval);
|
|
63
67
|
}, 5000);
|
|
64
68
|
|
|
@@ -69,5 +73,5 @@ export const useCookieMonitor = () => {
|
|
|
69
73
|
}
|
|
70
74
|
}, []);
|
|
71
75
|
|
|
72
|
-
return hasCookie;
|
|
76
|
+
return { hasCookie, timedOut };
|
|
73
77
|
};
|
|
@@ -3,4 +3,9 @@ import { ClientContext } from "../utils";
|
|
|
3
3
|
export type AuthRouteProps<T> =
|
|
4
4
|
| { authorized: true; data: T; clientContext: ClientContext; loading: false }
|
|
5
5
|
| { authorized: false; loading: false }
|
|
6
|
-
| {
|
|
6
|
+
| {
|
|
7
|
+
authorized: false;
|
|
8
|
+
loading: true;
|
|
9
|
+
data?: Partial<T>;
|
|
10
|
+
clientContext?: ClientContext;
|
|
11
|
+
};
|
|
@@ -2,7 +2,6 @@ import { useEffect, useRef, type ComponentType } from "react";
|
|
|
2
2
|
|
|
3
3
|
import { useRouter } from "next/router";
|
|
4
4
|
|
|
5
|
-
import { PageLoader } from "../components";
|
|
6
5
|
import { useCookieMonitor } from "../hooks/useCookieMonitor";
|
|
7
6
|
|
|
8
7
|
import type { AuthRouteProps } from "../types";
|
|
@@ -16,7 +15,7 @@ export const withAuth = <T extends Record<string, unknown>>(
|
|
|
16
15
|
) => {
|
|
17
16
|
return function AuthenticatedComponent(props: AuthRouteProps<T>) {
|
|
18
17
|
const router = useRouter();
|
|
19
|
-
const hasCookie = useCookieMonitor();
|
|
18
|
+
const { hasCookie, timedOut } = useCookieMonitor();
|
|
20
19
|
const reloadAttemptedRef = useRef(false);
|
|
21
20
|
|
|
22
21
|
// Debug logging
|
|
@@ -25,9 +24,10 @@ export const withAuth = <T extends Record<string, unknown>>(
|
|
|
25
24
|
authorized: props?.authorized,
|
|
26
25
|
loading: props?.loading,
|
|
27
26
|
hasCookie,
|
|
27
|
+
timedOut,
|
|
28
28
|
reloadAttempted: reloadAttemptedRef.current,
|
|
29
29
|
});
|
|
30
|
-
}, [props?.authorized, props?.loading, hasCookie]);
|
|
30
|
+
}, [props?.authorized, props?.loading, hasCookie, timedOut]);
|
|
31
31
|
|
|
32
32
|
useEffect(() => {
|
|
33
33
|
// If not authorized and not loading, reload the page
|
|
@@ -56,14 +56,28 @@ export const withAuth = <T extends Record<string, unknown>>(
|
|
|
56
56
|
}
|
|
57
57
|
}, [props?.loading, hasCookie, router]);
|
|
58
58
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
// If we're in loading state and timeout reached, redirect to home
|
|
61
|
+
if (props?.loading && timedOut && !reloadAttemptedRef.current) {
|
|
62
|
+
console.log("[withAuth] Cookie timeout - redirecting to home page");
|
|
63
|
+
reloadAttemptedRef.current = true;
|
|
64
|
+
router.push("/");
|
|
62
65
|
}
|
|
66
|
+
}, [props?.loading, timedOut, router]);
|
|
67
|
+
|
|
68
|
+
// Always render the component, but pass loading state
|
|
69
|
+
// This allows components to handle their own loading states
|
|
70
|
+
if (!props?.authorized && !props?.loading) {
|
|
71
|
+
// Not authorized and not loading = permanently denied
|
|
63
72
|
return null;
|
|
64
73
|
}
|
|
65
74
|
|
|
66
|
-
//
|
|
67
|
-
|
|
75
|
+
// Render with whatever data we have (might be empty/partial if loading)
|
|
76
|
+
const componentProps = {
|
|
77
|
+
...(props?.data || ({} as T)),
|
|
78
|
+
loading: props?.loading || false,
|
|
79
|
+
} as T & { loading: boolean };
|
|
80
|
+
|
|
81
|
+
return <Component {...componentProps} />;
|
|
68
82
|
};
|
|
69
83
|
};
|
|
@@ -2,7 +2,7 @@ import { useEffect, useRef, type ComponentType } from "react";
|
|
|
2
2
|
|
|
3
3
|
import { useRouter } from "next/router";
|
|
4
4
|
|
|
5
|
-
import { BuyerPortalProvider
|
|
5
|
+
import { BuyerPortalProvider } from "../components";
|
|
6
6
|
import { useCookieMonitor } from "../hooks/useCookieMonitor";
|
|
7
7
|
|
|
8
8
|
import type { AuthRouteProps } from "../types";
|
|
@@ -18,7 +18,7 @@ export const withAuthProvider = <T,>(
|
|
|
18
18
|
) => {
|
|
19
19
|
return function WrappedPage(props: AuthRouteProps<T>) {
|
|
20
20
|
const router = useRouter();
|
|
21
|
-
const hasCookie = useCookieMonitor();
|
|
21
|
+
const { hasCookie, timedOut } = useCookieMonitor();
|
|
22
22
|
const reloadAttemptedRef = useRef(false);
|
|
23
23
|
const isProduction = process.env.NODE_ENV === "production";
|
|
24
24
|
|
|
@@ -29,6 +29,7 @@ export const withAuthProvider = <T,>(
|
|
|
29
29
|
authorized: props?.authorized,
|
|
30
30
|
loading: props?.loading,
|
|
31
31
|
hasCookie,
|
|
32
|
+
timedOut,
|
|
32
33
|
reloadAttempted: reloadAttemptedRef.current,
|
|
33
34
|
routerPath: router.asPath,
|
|
34
35
|
});
|
|
@@ -37,6 +38,7 @@ export const withAuthProvider = <T,>(
|
|
|
37
38
|
props?.authorized,
|
|
38
39
|
props?.loading,
|
|
39
40
|
hasCookie,
|
|
41
|
+
timedOut,
|
|
40
42
|
router.asPath,
|
|
41
43
|
isProduction,
|
|
42
44
|
]);
|
|
@@ -71,17 +73,35 @@ export const withAuthProvider = <T,>(
|
|
|
71
73
|
}
|
|
72
74
|
}, [props?.loading, hasCookie, router]);
|
|
73
75
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
// If we're in loading state and timeout reached, redirect to home
|
|
78
|
+
if (props?.loading && timedOut && !reloadAttemptedRef.current) {
|
|
79
|
+
console.log(
|
|
80
|
+
"[withAuthProvider] Cookie timeout - redirecting to home page"
|
|
81
|
+
);
|
|
82
|
+
reloadAttemptedRef.current = true;
|
|
83
|
+
router.push("/");
|
|
78
84
|
}
|
|
85
|
+
}, [props?.loading, timedOut, router]);
|
|
86
|
+
|
|
87
|
+
// Always render the component, but pass loading state
|
|
88
|
+
// This allows components to handle their own loading states
|
|
89
|
+
if (!props?.authorized && !props?.loading) {
|
|
90
|
+
// Not authorized and not loading = permanently denied
|
|
79
91
|
return null;
|
|
80
92
|
}
|
|
81
93
|
|
|
94
|
+
// Render with whatever data we have (might be empty/partial if loading)
|
|
95
|
+
const componentProps = {
|
|
96
|
+
...(props?.data || ({} as T)),
|
|
97
|
+
loading: props?.loading || false,
|
|
98
|
+
} as T & { loading: boolean };
|
|
99
|
+
|
|
100
|
+
const clientContext = props?.clientContext || ({} as ClientContext);
|
|
101
|
+
|
|
82
102
|
return (
|
|
83
|
-
<BuyerPortalProvider clientContext={
|
|
84
|
-
<Component {...
|
|
103
|
+
<BuyerPortalProvider clientContext={clientContext}>
|
|
104
|
+
<Component {...componentProps} clientContext={clientContext} />
|
|
85
105
|
</BuyerPortalProvider>
|
|
86
106
|
);
|
|
87
107
|
};
|