1616 * under the License.
1717 */
1818
19- import { FC , useState , useEffect , useRef , ReactNode } from 'react' ;
20- import BaseSignIn , { BaseSignInProps } from './BaseSignIn' ;
19+ import { FC , useState , useEffect , useRef , ReactNode } from 'react' ;
20+ import BaseSignIn , { BaseSignInProps } from './BaseSignIn' ;
2121import useAsgardeo from '../../../../contexts/Asgardeo/useAsgardeo' ;
2222import {
2323 AsgardeoRuntimeError ,
@@ -27,7 +27,7 @@ import {
2727 EmbeddedSignInFlowRequestV2 ,
2828 EmbeddedSignInFlowStatusV2 ,
2929} from '@asgardeo/browser' ;
30- import { normalizeFlowResponse } from './transformer' ;
30+ import { normalizeFlowResponse } from './transformer' ;
3131import useTranslation from '../../../../hooks/useTranslation' ;
3232
3333/**
@@ -164,9 +164,9 @@ export type SignInProps = {
164164 * };
165165 * ```
166166 */
167- const SignIn : FC < SignInProps > = ( { className, size = 'medium' , onSuccess, onError, variant, children} ) => {
168- const { applicationId, afterSignInUrl, signIn, isInitialized, isLoading, baseUrl} = useAsgardeo ( ) ;
169- const { t } = useTranslation ( ) ;
167+ const SignIn : FC < SignInProps > = ( { className, size = 'medium' , onSuccess, onError, variant, children } ) => {
168+ const { applicationId, afterSignInUrl, signIn, isInitialized, isLoading, baseUrl } = useAsgardeo ( ) ;
169+ const { t } = useTranslation ( ) ;
170170
171171 // State management for the flow
172172 const [ components , setComponents ] = useState < EmbeddedFlowComponent [ ] > ( [ ] ) ;
@@ -186,13 +186,22 @@ const SignIn: FC<SignInProps> = ({className, size = 'medium', onSuccess, onError
186186
187187 /**
188188 * Initialize the authentication flow.
189+ * Priority: flowId > applicationId (from context) > applicationId (from URL)
189190 */
190191 const initializeFlow = async ( ) : Promise < void > => {
191- const applicationIdFromUrl : string = new URL ( window . location . href ) . searchParams . get ( 'applicationId' ) ;
192+ const urlParams = new URL ( window . location . href ) . searchParams ;
193+ const flowIdFromUrl = urlParams . get ( 'flowId' ) ;
194+ const applicationIdFromUrl = urlParams . get ( 'applicationId' ) ;
192195
193- if ( ! applicationIdFromUrl && ! applicationId ) {
196+ // Priority order: flowId from URL > applicationId from context > applicationId from URL
197+ const effectiveFlowId = flowIdFromUrl ;
198+ const effectiveApplicationId = applicationId || applicationIdFromUrl ;
199+
200+ console . log ( 'Effective Flow ID:' , effectiveFlowId ) ;
201+ // Validate that we have either flowId or applicationId
202+ if ( ! effectiveFlowId && ! effectiveApplicationId ) {
194203 const error = new AsgardeoRuntimeError (
195- `Application ID is required for authentication` ,
204+ 'Either flowId or applicationId is required for authentication' ,
196205 'SignIn-initializeFlow-RuntimeError-001' ,
197206 'react' ,
198207 'Something went wrong while trying to sign in. Please try again later.' ,
@@ -203,12 +212,22 @@ const SignIn: FC<SignInProps> = ({className, size = 'medium', onSuccess, onError
203212
204213 try {
205214 setFlowError ( null ) ;
206- const response : EmbeddedSignInFlowResponseV2 = await signIn ( {
207- applicationId : applicationId || applicationIdFromUrl ,
208- flowType : EmbeddedFlowType . Authentication ,
209- } ) as EmbeddedSignInFlowResponseV2 ;
210215
211- const { flowId, components} = normalizeFlowResponse ( response , t ) ;
216+ let response : EmbeddedSignInFlowResponseV2 ;
217+
218+ // Use flowId if available (priority), otherwise use applicationId
219+ if ( effectiveFlowId ) {
220+ response = await signIn ( {
221+ flowId : effectiveFlowId ,
222+ } ) as EmbeddedSignInFlowResponseV2 ;
223+ } else {
224+ response = await signIn ( {
225+ applicationId : effectiveApplicationId ,
226+ flowType : EmbeddedFlowType . Authentication ,
227+ } ) as EmbeddedSignInFlowResponseV2 ;
228+ }
229+
230+ const { flowId, components } = normalizeFlowResponse ( response , t ) ;
212231
213232 if ( flowId && components ) {
214233 setCurrentFlowId ( flowId ) ;
@@ -246,7 +265,7 @@ const SignIn: FC<SignInProps> = ({className, size = 'medium', onSuccess, onError
246265 ...payload ,
247266 } ) as EmbeddedSignInFlowResponseV2 ;
248267
249- const { flowId, components} = normalizeFlowResponse ( response , t ) ;
268+ const { flowId, components } = normalizeFlowResponse ( response , t ) ;
250269
251270 if ( response . flowStatus === EmbeddedSignInFlowStatusV2 . Complete ) {
252271 onSuccess &&
0 commit comments