11import type { ReactElement } from 'react' ;
2- import React , { useEffect , useMemo , useRef } from 'react' ;
2+ import React , { useCallback , useEffect , useMemo , useRef } from 'react' ;
33import { useRouter } from 'next/router' ;
44import classNames from 'classnames' ;
55import {
@@ -29,7 +29,11 @@ import { broadcastChannel } from '../../../lib/constants';
2929import Logo , { LogoPosition } from '../../../components/Logo' ;
3030import type { LoggedUser } from '../../../lib/user' ;
3131import { FunnelStepTransitionType } from '../types/funnel' ;
32- import { sanitizeMessage , shouldRedirectAuth } from '../shared' ;
32+ import {
33+ AUTH_REDIRECT_KEY ,
34+ sanitizeMessage ,
35+ shouldRedirectAuth ,
36+ } from '../shared' ;
3337import type { FunnelStepSignup } from '../types/funnel' ;
3438import { useConsentCookie } from '../../../hooks/useCookieConsent' ;
3539import { GdprConsentKey } from '../../../hooks/useCookieBanner' ;
@@ -50,15 +54,9 @@ const useRegistrationListeners = (
5054 const { logEvent } = useLogContext ( ) ;
5155 const router = useRouter ( ) ;
5256
53- const onProviderMessage = async ( e : MessageEvent ) => {
54- const isEventSupported = supportedEvents . includes ( e . data ?. eventKey ) ;
55-
56- if ( ! isEventSupported ) {
57- return undefined ;
58- }
59-
60- if ( e . data ?. flow ) {
61- const connected = await getKratosFlow ( AuthFlow . Registration , e . data . flow ) ;
57+ const handleExistingFlow = useCallback (
58+ async ( flow : string ) => {
59+ const connected = await getKratosFlow ( AuthFlow . Registration , flow ) ;
6260
6361 logEvent ( {
6462 event_name : AuthEventNames . RegistrationError ,
@@ -78,6 +76,19 @@ const useRegistrationListeners = (
7876 }
7977
8078 return displayToast ( `${ labels . auth . error . generic } Code: ${ code } ` ) ;
79+ } ,
80+ [ displayToast , logEvent ] ,
81+ ) ;
82+
83+ const onProviderMessage = async ( e : MessageEvent ) => {
84+ const isEventSupported = supportedEvents . includes ( e . data ?. eventKey ) ;
85+
86+ if ( ! isEventSupported ) {
87+ return undefined ;
88+ }
89+
90+ if ( e . data ?. flow ) {
91+ return handleExistingFlow ( e . data . flow ) ;
8192 }
8293
8394 const bootResponse = await refetchBoot ( ) ;
@@ -100,6 +111,23 @@ const useRegistrationListeners = (
100111 useEventListener ( broadcastChannel , 'message' , onProviderMessage ) ;
101112
102113 useEventListener ( globalThis , 'message' , onProviderMessage ) ;
114+
115+ useEffect ( ( ) => {
116+ if ( ! router ?. isReady || ! router ?. query ?. flow ) {
117+ return ;
118+ }
119+
120+ const flowFn = async ( ) => {
121+ await handleExistingFlow ( router . query . flow as string ) ;
122+ const url = new URL ( window . location . href ) ;
123+ const fullPath = url . origin + url . pathname ;
124+ const { flow, ...rest } = router . query ;
125+ const params = new URLSearchParams ( rest as Record < string , string > ) ;
126+ router . replace ( `${ fullPath } ?${ params } ` ) ;
127+ } ;
128+
129+ flowFn ( ) ;
130+ } , [ handleExistingFlow , router ] ) ;
103131} ;
104132
105133function InnerFunnelRegistration ( {
@@ -123,6 +151,7 @@ function InnerFunnelRegistration({
123151 } ,
124152 onRedirect : ( redirect ) => {
125153 if ( shouldRedirect ) {
154+ window . sessionStorage . setItem ( AUTH_REDIRECT_KEY , window . location . href ) ;
126155 window . location . href = redirect ;
127156 } else {
128157 windowPopup . current . location . href = redirect ;
@@ -142,6 +171,14 @@ function InnerFunnelRegistration({
142171
143172 const sanitizedHeading = useMemo ( ( ) => sanitizeMessage ( headline ) , [ headline ] ) ;
144173
174+ useEffect ( ( ) => {
175+ if ( typeof window === 'undefined' || ! shouldRedirect ) {
176+ return ;
177+ }
178+
179+ window . sessionStorage . removeItem ( AUTH_REDIRECT_KEY ) ;
180+ } , [ shouldRedirect ] ) ;
181+
145182 return (
146183 < div className = "relative flex w-full flex-1 flex-col items-center justify-center overflow-hidden" >
147184 < div className = "absolute inset-0" >
0 commit comments