diff --git a/src/state/KindeProvider.tsx b/src/state/KindeProvider.tsx index ed718ff..c4b6bd0 100644 --- a/src/state/KindeProvider.tsx +++ b/src/state/KindeProvider.tsx @@ -102,6 +102,7 @@ type KindeProviderProps = { children: React.ReactNode; clientId: string; domain: string; + authorizationEndpoint?: string; /** * Use localstorage for refresh token. * @@ -205,6 +206,7 @@ export const KindeProvider = ({ clientId, children, domain, + authorizationEndpoint, useInsecureForRefreshToken = false, redirectUri, callbacks = {}, @@ -341,6 +343,23 @@ export const KindeProvider = ({ }); const initRef = useRef(false); + /** + * Helper function to construct the final auth URL with optional custom authorization endpoint + */ + const buildAuthUrl = useCallback((authUrl: { url: URL }): string => { + if (!authorizationEndpoint) { + return authUrl.url.toString(); + } + + const customUrl = new URL(authUrl.url.toString()); + // Ensure it's a path, not a full URL + customUrl.pathname = authorizationEndpoint.startsWith("/") + ? authorizationEndpoint + : `/${authorizationEndpoint}`; + + return customUrl.toString(); + }, [authorizationEndpoint]); + const login = useCallback( async ( options: LoginMethodParams & { state?: Record } = {}, @@ -370,9 +389,11 @@ export const KindeProvider = ({ authProps, ); + const finalAuthUrl = buildAuthUrl(authUrl); + try { navigateToKinde({ - url: authUrl.url.toString(), + url: finalAuthUrl, popupOptions, handleResult: processAuthResult, }); @@ -427,9 +448,12 @@ export const KindeProvider = ({ IssuerRouteTypes.register, authProps, ); + + const finalAuthUrl = buildAuthUrl(authUrl); + try { navigateToKinde({ - url: authUrl.url.toString(), + url: finalAuthUrl, popupOptions, handleResult: processAuthResult, }); @@ -455,7 +479,7 @@ export const KindeProvider = ({ ); } }, - [redirectUri, popupOptions, mergedCallbacks, audience, clientId, domain], + [redirectUri, popupOptions, mergedCallbacks, audience, clientId, domain, buildAuthUrl], ); const logout = useCallback( @@ -764,7 +788,7 @@ export const KindeProvider = ({ } const hasCode = params.has("code"); - const isOnRedirectUri = window.location.href.startsWith(redirectUri); + const isOnRedirectUri = window.location.href?.startsWith(redirectUri) ?? false; if (!hasCode || !isOnRedirectUri) { try { const user = await getUserProfile();