Skip to content

Commit 0a1f58b

Browse files
committed
Add tanstack router
1 parent 5a85761 commit 0a1f58b

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com).
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
import {Callback} from '@asgardeo/react';
20+
import {useRouter, useRouterState} from '@tanstack/react-router';
21+
import {FC} from 'react';
22+
23+
/**
24+
* Props for the CallbackRoute component.
25+
*/
26+
export interface CallbackRouteProps {
27+
/**
28+
* Callback function called when an error occurs during OAuth processing.
29+
* @param error - The error that occurred
30+
*/
31+
onError?: (error: Error) => void;
32+
33+
/**
34+
* Optional custom navigation handler.
35+
* If provided, this will be called instead of the default navigate() behavior.
36+
* Useful for apps that need custom navigation logic.
37+
* @param path - The path to navigate to
38+
*/
39+
onNavigate?: (path: string) => void;
40+
}
41+
42+
/**
43+
* Handles OAuth callback redirects for TanStack Router applications.
44+
* Processes authorization code, validates CSRF state, and navigates back to the original path.
45+
* Automatically handles TanStack Router basepath when configured.
46+
*
47+
* @example
48+
* ```tsx
49+
* const callbackRoute = createRoute({
50+
* getParentRoute: () => rootRoute,
51+
* path: '/callback',
52+
* component: CallbackRoute,
53+
* });
54+
* ```
55+
*/
56+
const CallbackRoute: FC<CallbackRouteProps> = ({onError, onNavigate}: CallbackRouteProps) => {
57+
const router: ReturnType<typeof useRouter> = useRouter();
58+
const routerState: ReturnType<typeof useRouterState> = useRouterState();
59+
const {pathname}: {pathname: string} = routerState.location;
60+
61+
const handleNavigate = (path: string): void => {
62+
if (onNavigate) {
63+
onNavigate(path);
64+
return;
65+
}
66+
67+
const fullPath: string = window.location.pathname;
68+
const basename: string = fullPath.endsWith(pathname) ? fullPath.slice(0, -pathname.length).replace(/\/$/, '') : '';
69+
70+
const navigationPath: string = basename && path.startsWith(basename) ? path.slice(basename.length) || '/' : path;
71+
72+
router.navigate({to: navigationPath}).catch(() => {});
73+
};
74+
75+
return (
76+
<Callback
77+
onNavigate={handleNavigate}
78+
onError={
79+
onError ||
80+
((error: Error): void => {
81+
// eslint-disable-next-line no-console
82+
console.error('OAuth callback error:', error);
83+
})
84+
}
85+
/>
86+
);
87+
};
88+
89+
export default CallbackRoute;

packages/tanstack-router/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@
1818

1919
export {default as ProtectedRoute} from './components/ProtectedRoute';
2020
export * from './components/ProtectedRoute';
21+
22+
export {default as CallbackRoute} from './components/CallbackRoute';
23+
export * from './components/CallbackRoute';

0 commit comments

Comments
 (0)