@@ -43,28 +43,6 @@ function urlBase64ToUint8Array(base64String) {
4343 return outputArray
4444}
4545
46- const refreshSessionAndReload = async ( ) => {
47- try {
48- const res = await fetch ( "/api/auth/refresh-session" ) ;
49- if ( ! res . ok ) {
50- throw new Error ( "Failed to refresh session" ) ;
51- }
52- // Once the session cookie is updated on the server, reload the page.
53- // The browser will send the new cookie, and the server will render with the updated user role.
54- window . location . reload ( ) ;
55- } catch ( error ) {
56- console . error ( "Session refresh failed:" , error ) ;
57- // Optionally, handle the error, e.g., show a message to the user.
58- // For simplicity, we can still fall back to the old method if refresh fails.
59- const logoutUrl = new URL ( "/auth/logout" , window . location . origin ) ;
60- logoutUrl . searchParams . set (
61- "returnTo" ,
62- `${ process . env . NEXT_PUBLIC_APP_BASE_URL } `
63- ) ;
64- window . location . assign ( logoutUrl . toString ( ) ) ;
65- }
66- } ;
67-
6846export default function LayoutWrapper ( { children } ) {
6947 // ... (keep all your existing state declarations)
7048 const [ isNotificationsOpen , setNotificationsOpen ] = useState ( false )
@@ -78,6 +56,7 @@ export default function LayoutWrapper({ children }) {
7856 const wsRef = useRef ( null )
7957 const pathname = usePathname ( )
8058 const router = useRouter ( )
59+ const searchParams = useSearchParams ( ) // Hook to read URL query parameters
8160 const posthog = usePostHog ( )
8261
8362 const { user, error : authError , isLoading : isAuthLoading } = useUser ( )
@@ -97,18 +76,58 @@ export default function LayoutWrapper({ children }) {
9776 } , [ user , posthog ] )
9877
9978 useEffect ( ( ) => {
100- const urlParams = new URLSearchParams ( window . location . search )
79+ const paymentStatus = searchParams . get ( "payment_status" )
80+ const needsRefresh = searchParams . get ( "refresh_session" )
10181
102- // Handle post-upgrade: force a session refresh
103- if ( urlParams . get ( "refresh_session" ) === "true" ) {
82+ if ( paymentStatus === "success" && posthog ) {
83+ posthog . capture ( "plan_upgraded" , {
84+ plan_name : "pro"
85+ // MRR and billing_cycle are not available on the client
86+ } )
87+ }
88+ // Check for either trigger
89+ if ( paymentStatus === "success" || needsRefresh === "true" ) {
90+ // CRITICAL FIX: Clean the URL synchronously *before* doing anything else.
91+ // This prevents the refresh loop.
92+ window . history . replaceState ( null , "" , pathname )
10493 const toastId = toast . loading ( "Updating your session..." , {
10594 duration : 4000
10695 } )
107- // Remove the query params from the URL to avoid re-triggering on reload
108- window . history . replaceState ( null , "" , pathname )
109- refreshSessionAndReload ( )
96+
97+ // const refreshSession = async () => {
98+ // const toastId = toast.loading("Updating your session...", {
99+ // duration: 4000
100+ // })
101+ // try {
102+ // // Call the API to get a new session cookie
103+ // const res = await fetch("/api/auth/refresh-session")
104+ // if (!res.ok) {
105+ // const errorData = await res.json()
106+ // throw new Error(
107+ // errorData.error || "Session refresh failed."
108+ // )
109+ // }
110+
111+ // // Now that the cookie is updated and the URL is clean, reload the page.
112+ // // This will re-run server components and hooks with the new session data.
113+ // window.location.reload()
114+ // } catch (error) {
115+ // toast.error(
116+ // `Failed to refresh session: ${error.message}. Please log in again to see your new plan.`,
117+ // { id: toastId }
118+ // )
119+ // }
120+ // }
121+ // refreshSession()
122+
123+ const logoutUrl = new URL ( "/auth/logout" , window . location . origin )
124+ logoutUrl . searchParams . set (
125+ "returnTo" ,
126+ process . env . NEXT_PUBLIC_APP_BASE_URL
127+ )
128+ window . location . assign ( logoutUrl . toString ( ) )
110129 }
111- } , [ ] )
130+ } , [ searchParams , router , pathname ] ) // Dependencies are correct
112131
113132 // ... (keep the rest of your useEffects and functions exactly as they were)
114133 useEffect ( ( ) => {
0 commit comments