Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions src/state/KindeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type KindeProviderProps = {
children: React.ReactNode;
clientId: string;
domain: string;
authorizationEndpoint?: string;
/**
* Use localstorage for refresh token.
*
Expand Down Expand Up @@ -139,6 +140,7 @@ export const KindeProvider = ({
clientId,
children,
domain,
authorizationEndpoint,
useInsecureForRefreshToken = false,
redirectUri,
callbacks = {},
Expand Down Expand Up @@ -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<string, string> } = {},
Expand Down Expand Up @@ -221,9 +240,11 @@ export const KindeProvider = ({
authProps,
);

const finalAuthUrl = buildAuthUrl(authUrl);

try {
navigateToKinde({
url: authUrl.url.toString(),
url: finalAuthUrl,
popupOptions,
handleResult: processAuthResult,
});
Expand All @@ -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(
Expand Down Expand Up @@ -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,
});
Expand All @@ -298,7 +322,7 @@ export const KindeProvider = ({
);
}
},
[redirectUri, popupOptions, mergedCallbacks, audience, clientId, domain],
[redirectUri, popupOptions, mergedCallbacks, audience, clientId, domain, buildAuthUrl],
);

const logout = useCallback(
Expand Down Expand Up @@ -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();
Expand Down