diff --git a/.changeset/tough-ads-grow.md b/.changeset/tough-ads-grow.md new file mode 100644 index 00000000..f33ac5d5 --- /dev/null +++ b/.changeset/tough-ads-grow.md @@ -0,0 +1,5 @@ +--- +'@asgardeo/react': patch +--- + +Handle code exchange for V2 diff --git a/packages/react/src/contexts/Asgardeo/AsgardeoProvider.tsx b/packages/react/src/contexts/Asgardeo/AsgardeoProvider.tsx index 1e887c58..040bf646 100644 --- a/packages/react/src/contexts/Asgardeo/AsgardeoProvider.tsx +++ b/packages/react/src/contexts/Asgardeo/AsgardeoProvider.tsx @@ -156,14 +156,28 @@ const AsgardeoProvider: FC> = ({ const isV2Platform = config.platform === Platform.AsgardeoV2; - if (hasAuthParamsResult && !isV2Platform) { + if (hasAuthParamsResult) { try { - await signIn( - {callOnlyOnRedirect: true}, - // authParams?.authorizationCode, - // authParams?.sessionState, - // authParams?.state, - ); + if (isV2Platform) { + // For V2 platform, check if this is an embedded flow or traditional OAuth + const urlParams = currentUrl.searchParams; + const code = urlParams.get('code'); + const flowIdFromUrl = urlParams.get('flowId'); + const storedFlowId = sessionStorage.getItem('asgardeo_flow_id'); + + // If there's a code and no flowId, exchange OAuth code for tokens + if (code && !flowIdFromUrl && !storedFlowId) { + await signIn(); + } + } else { + // If non-V2 platform, use traditional OAuth callback handling + await signIn( + {callOnlyOnRedirect: true}, + // authParams?.authorizationCode, + // authParams?.sessionState, + // authParams?.state, + ); + } // setError(null); } catch (error) { if (error && Object.prototype.hasOwnProperty.call(error, 'code')) {