Skip to content

Commit 9e5a298

Browse files
committed
Update v2 hook
1 parent 19bb98c commit 9e5a298

8 files changed

Lines changed: 43 additions & 29 deletions

File tree

packages/react-router/src/components/Callback.tsx renamed to packages/react-router/src/components/CallbackRoute.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
2+
* Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com).
33
*
44
* WSO2 LLC. licenses this file to you under the Apache License,
55
* Version 2.0 (the "License"); you may not use this file except
@@ -16,14 +16,14 @@
1616
* under the License.
1717
*/
1818

19-
import {BaseCallback} from '@asgardeo/react';
19+
import {Callback} from '@asgardeo/react';
2020
import {FC} from 'react';
2121
import {useLocation, useNavigate} from 'react-router';
2222

2323
/**
24-
* Props for the Callback component.
24+
* Props for the CallbackRoute component.
2525
*/
26-
export interface CallbackProps {
26+
export interface CallbackRouteProps {
2727
/**
2828
* Callback function called when an error occurs during OAuth processing.
2929
* @param error - The error that occurred
@@ -46,10 +46,10 @@ export interface CallbackProps {
4646
*
4747
* @example
4848
* ```tsx
49-
* <Route path="/callback" element={<Callback />} />
49+
* <Route path="/callback" element={<CallbackRoute />} />
5050
* ```
5151
*/
52-
const Callback: FC<CallbackProps> = ({onError, onNavigate}: CallbackProps) => {
52+
const CallbackRoute: FC<CallbackRouteProps> = ({onError, onNavigate}: CallbackRouteProps) => {
5353
const navigate: ReturnType<typeof useNavigate> = useNavigate();
5454
const location: ReturnType<typeof useLocation> = useLocation();
5555

@@ -71,7 +71,7 @@ const Callback: FC<CallbackProps> = ({onError, onNavigate}: CallbackProps) => {
7171
};
7272

7373
return (
74-
<BaseCallback
74+
<Callback
7575
onNavigate={handleNavigate}
7676
onError={
7777
onError ||
@@ -84,4 +84,4 @@ const Callback: FC<CallbackProps> = ({onError, onNavigate}: CallbackProps) => {
8484
);
8585
};
8686

87-
export default Callback;
87+
export default CallbackRoute;

packages/react-router/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
export {default as ProtectedRoute} from './components/ProtectedRoute';
2020
export * from './components/ProtectedRoute';
2121

22-
export {default as Callback} from './components/Callback';
23-
export * from './components/Callback';
22+
export {default as CallbackRoute} from './components/CallbackRoute';
23+
export * from './components/CallbackRoute';

packages/react/src/components/auth/Callback/BaseCallback.tsx renamed to packages/react/src/components/auth/Callback/Callback.tsx

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
2+
* Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com).
33
*
44
* WSO2 LLC. licenses this file to you under the Apache License,
55
* Version 2.0 (the "License"); you may not use this file except
@@ -16,30 +16,33 @@
1616
* under the License.
1717
*/
1818

19+
import {navigate as browserNavigate} from '@asgardeo/browser';
1920
import {FC, useEffect, useRef} from 'react';
2021

2122
/**
22-
* Props for BaseCallback component
23+
* Props for Callback component
2324
*/
24-
export interface BaseCallbackProps {
25+
export interface CallbackProps {
2526
/**
2627
* Callback function called when an error occurs
2728
*/
2829
onError?: (error: Error) => void;
2930

3031
/**
31-
* Function to navigate to a different path
32+
* Function to navigate to a different path.
33+
* If not provided, falls back to the browser navigate utility (SPA navigation via History API for same-origin paths).
34+
* Provide this prop to enable framework-specific navigation (e.g., from React Router).
3235
*/
33-
onNavigate: (path: string) => void;
36+
onNavigate?: (path: string) => void;
3437
}
3538

3639
/**
3740
* BaseCallback is a headless component that handles OAuth callback parameter forwarding.
3841
* This component extracts OAuth parameters (code, state, error) from the URL and forwards them
3942
* to the original component that initiated the OAuth flow.
4043
*
41-
* This component is framework-agnostic and should be wrapped by framework-specific
42-
* implementations that provide navigation functions.
44+
* Works standalone using the browser navigate utility (History API) for navigation by default.
45+
* Pass an onNavigate prop to enable framework-specific navigation (e.g., via React Router).
4346
*
4447
* Flow: Extract OAuth parameters from URL -> Parse state parameter -> Redirect to original path with parameters
4548
*
@@ -49,10 +52,19 @@ export interface BaseCallbackProps {
4952
* - Handling the assertion and auth/callback POST
5053
* - Managing the authenticated session
5154
*/
52-
export const BaseCallback: FC<BaseCallbackProps> = ({onNavigate, onError}: BaseCallbackProps) => {
55+
export const Callback: FC<CallbackProps> = ({onNavigate, onError}: CallbackProps) => {
5356
// Prevent double execution in React Strict Mode
5457
const processingRef: any = useRef(false);
5558

59+
// Resolve navigation: use provided onNavigate (router-aware) or fall back to browser navigate utility
60+
const navigate = (path: string): void => {
61+
if (onNavigate) {
62+
onNavigate(path);
63+
} else {
64+
browserNavigate(path);
65+
}
66+
};
67+
5668
useEffect(() => {
5769
const processOAuthCallback = (): void => {
5870
// Guard against double execution
@@ -92,7 +104,7 @@ export const BaseCallback: FC<BaseCallbackProps> = ({onNavigate, onError}: BaseC
92104
params.set('error_description', errorDescription);
93105
}
94106

95-
onNavigate(`/?${params.toString()}`);
107+
navigate(`/?${params.toString()}`);
96108
return;
97109
}
98110
throw new Error('Invalid OAuth state - possible CSRF attack');
@@ -123,7 +135,7 @@ export const BaseCallback: FC<BaseCallbackProps> = ({onNavigate, onError}: BaseC
123135
params.set('error_description', errorDescription);
124136
}
125137

126-
onNavigate(`${returnPath}?${params.toString()}`);
138+
navigate(`${returnPath}?${params.toString()}`);
127139
return;
128140
}
129141

@@ -140,7 +152,7 @@ export const BaseCallback: FC<BaseCallbackProps> = ({onNavigate, onError}: BaseC
140152
params.set('nonce', nonce);
141153
}
142154

143-
onNavigate(`${returnPath}?${params.toString()}`);
155+
navigate(`${returnPath}?${params.toString()}`);
144156
} catch (err) {
145157
const errorMessage: string = err instanceof Error ? err.message : 'OAuth callback processing failed';
146158
// eslint-disable-next-line no-console
@@ -153,7 +165,7 @@ export const BaseCallback: FC<BaseCallbackProps> = ({onNavigate, onError}: BaseC
153165
params.set('error', 'callback_error');
154166
params.set('error_description', errorMessage);
155167

156-
onNavigate(`${returnPath}?${params.toString()}`);
168+
navigate(`${returnPath}?${params.toString()}`);
157169
}
158170
};
159171

@@ -164,4 +176,4 @@ export const BaseCallback: FC<BaseCallbackProps> = ({onNavigate, onError}: BaseC
164176
return null;
165177
};
166178

167-
export default BaseCallback;
179+
export default Callback;

packages/react/src/components/presentation/auth/AcceptInvite/v2/BaseAcceptInvite.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {cx} from '@emotion/css';
2020
import {FC, ReactElement, ReactNode, useCallback, useEffect, useRef, useState} from 'react';
2121
import useStyles from './BaseAcceptInvite.styles';
2222
import useTheme from '../../../../../contexts/Theme/useTheme';
23-
import {useOAuthCallback} from '../../../../../hooks/useOAuthCallback';
23+
import {useOAuthCallback} from '../../../../../hooks/v2/useOAuthCallback';
2424
import useTranslation from '../../../../../hooks/useTranslation';
2525
import {initiateOAuthRedirect} from '../../../../../utils/oauth';
2626
import {normalizeFlowResponse, extractErrorMessage} from '../../../../../utils/v2/flowTransformer';

packages/react/src/components/presentation/auth/SignIn/v2/SignIn.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {FC, ReactElement, useState, useEffect, useRef, ReactNode} from 'react';
2929
// eslint-disable-next-line import/no-named-as-default
3030
import BaseSignIn, {BaseSignInProps} from './BaseSignIn';
3131
import useAsgardeo from '../../../../../contexts/Asgardeo/useAsgardeo';
32-
import {useOAuthCallback} from '../../../../../hooks/useOAuthCallback';
32+
import {useOAuthCallback} from '../../../../../hooks/v2/useOAuthCallback';
3333
import useTranslation from '../../../../../hooks/useTranslation';
3434
import {initiateOAuthRedirect} from '../../../../../utils/oauth';
3535
import {normalizeFlowResponse} from '../../../../../utils/v2/flowTransformer';

packages/react/src/hooks/useOAuthCallback.ts renamed to packages/react/src/hooks/v2/useOAuthCallback.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
2+
* Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com).
33
*
44
* WSO2 LLC. licenses this file to you under the Apache License,
55
* Version 2.0 (the "License"); you may not use this file except

packages/react/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export * from './components/presentation/auth/InviteUser';
125125

126126
export {BaseAcceptInvite, AcceptInvite} from './components/presentation/auth/AcceptInvite';
127127

128-
export * from './components/auth/Callback/BaseCallback';
128+
export * from './components/auth/Callback/Callback';
129129

130130
// Sign-In Options
131131
export {default as IdentifierFirst} from './components/presentation/auth/SignIn/v1/options/IdentifierFirst';

packages/react/src/utils/oauth.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
2+
* Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com).
33
*
44
* WSO2 LLC. licenses this file to you under the Apache License,
55
* Version 2.0 (the "License"); you may not use this file except
@@ -16,6 +16,8 @@
1616
* under the License.
1717
*/
1818

19+
import {navigate} from '@asgardeo/browser';
20+
1921
/**
2022
* Initiates OAuth redirect with CSRF protection.
2123
* Generates random state, stores return path in sessionStorage, and redirects to OAuth provider.
@@ -43,5 +45,5 @@ export function initiateOAuthRedirect(redirectURL: string): void {
4345
const redirectUrlObj: URL = new URL(redirectURL);
4446
redirectUrlObj.searchParams.set('state', state);
4547

46-
window.location.href = redirectUrlObj.toString();
48+
navigate(redirectUrlObj.toString());
4749
}

0 commit comments

Comments
 (0)