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
1616 * under the License.
1717 */
1818
19+ import { navigate as browserNavigate } from '@asgardeo/browser' ;
1920import { 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 ;
0 commit comments