diff --git a/src/state/KindeProvider.tsx b/src/state/KindeProvider.tsx index d6a5a8c..333840e 100644 --- a/src/state/KindeProvider.tsx +++ b/src/state/KindeProvider.tsx @@ -94,6 +94,7 @@ type KindeProviderProps = { children: React.ReactNode; clientId: string; domain: string; + authorizationEndpoint?: string; /** * Use localstorage for refresh token. * @@ -139,6 +140,7 @@ export const KindeProvider = ({ clientId, children, domain, + authorizationEndpoint, useInsecureForRefreshToken = false, redirectUri, callbacks = {}, @@ -192,6 +194,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 } = {}, @@ -221,9 +240,11 @@ export const KindeProvider = ({ authProps, ); + const finalAuthUrl = buildAuthUrl(authUrl); + try { navigateToKinde({ - url: authUrl.url.toString(), + url: finalAuthUrl, popupOptions, handleResult: processAuthResult, }); @@ -238,7 +259,7 @@ export const KindeProvider = ({ ); } }, - [audience, clientId, redirectUri, popupOptions, mergedCallbacks, domain, scope], + [audience, clientId, redirectUri, popupOptions, mergedCallbacks, domain, scope, buildAuthUrl], ); const register = useCallback( @@ -270,9 +291,12 @@ export const KindeProvider = ({ IssuerRouteTypes.register, authProps, ); + + const finalAuthUrl = buildAuthUrl(authUrl); + try { navigateToKinde({ - url: authUrl.url.toString(), + url: finalAuthUrl, popupOptions, handleResult: processAuthResult, }); @@ -298,7 +322,7 @@ export const KindeProvider = ({ ); } }, - [redirectUri, popupOptions, mergedCallbacks, audience, clientId, domain], + [redirectUri, popupOptions, mergedCallbacks, audience, clientId, domain, buildAuthUrl], ); const logout = useCallback( @@ -604,7 +628,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();