@@ -43,6 +43,28 @@ 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+
4668export default function LayoutWrapper ( { children } ) {
4769 // ... (keep all your existing state declarations)
4870 const [ isNotificationsOpen , setNotificationsOpen ] = useState ( false )
@@ -56,7 +78,6 @@ export default function LayoutWrapper({ children }) {
5678 const wsRef = useRef ( null )
5779 const pathname = usePathname ( )
5880 const router = useRouter ( )
59- const searchParams = useSearchParams ( ) // Hook to read URL query parameters
6081 const posthog = usePostHog ( )
6182
6283 const { user, error : authError , isLoading : isAuthLoading } = useUser ( )
@@ -76,58 +97,18 @@ export default function LayoutWrapper({ children }) {
7697 } , [ user , posthog ] )
7798
7899 useEffect ( ( ) => {
79- const paymentStatus = searchParams . get ( "payment_status" )
80- const needsRefresh = searchParams . get ( "refresh_session" )
100+ const urlParams = new URLSearchParams ( window . location . search )
81101
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 )
102+ // Handle post-upgrade: force a session refresh
103+ if ( urlParams . get ( "refresh_session" ) === "true" ) {
93104 const toastId = toast . loading ( "Updating your session..." , {
94105 duration : 4000
95106 } )
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 ( ) )
107+ // Remove the query params from the URL to avoid re-triggering on reload
108+ window . history . replaceState ( null , "" , pathname )
109+ refreshSessionAndReload ( )
129110 }
130- } , [ searchParams , router , pathname ] ) // Dependencies are correct
111+ } , [ ] )
131112
132113 // ... (keep the rest of your useEffects and functions exactly as they were)
133114 useEffect ( ( ) => {
0 commit comments