Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix authProvider hooks support for redirectTo: absolute URL #8382

Merged
merged 3 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
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
19 changes: 14 additions & 5 deletions packages/ra-core/src/auth/useLogout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,20 @@ const useLogout = (): Logout => {
// do not redirect
return;
}
// redirectTo can contain a query string, e.g. '/login?foo=bar'
// we must split the redirectTo to pass a structured location to navigate()
const redirectToParts = (
redirectToFromProvider || redirectTo
).split('?');

const finalRedirectTo = redirectToFromProvider || redirectTo;

if (finalRedirectTo?.startsWith('http')) {
// absolute link (e.g. https://my.oidc.server/login)
resetStore();
queryClient.clear();
window.location.href = finalRedirectTo;
return finalRedirectTo;
}

// redirectTo is an internal location that may contain a query string, e.g. '/login?foo=bar'
// we must split it to pass a structured location to navigate()
const redirectToParts = finalRedirectTo.split('?');
const newLocation: Partial<Path> = {
pathname: redirectToParts[0],
};
Expand Down
24 changes: 16 additions & 8 deletions packages/ra-core/src/auth/useLogoutIfAccessDenied.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,18 @@ const useLogoutIfAccessDenied = (): LogoutIfAccessDenied => {
timer = undefined;
}, 0);

const redirectTo =
e && e.redirectTo != null
? e.redirectTo
: error && error.redirectTo
? error.redirectTo
: undefined;

const shouldNotify = !(
disableNotification ||
(e && e.message === false) ||
(error && error.message === false)
(error && error.message === false) ||
redirectTo?.startsWith('http')
);
if (shouldNotify) {
// notify only if not yet logged out
Expand All @@ -90,17 +98,17 @@ const useLogoutIfAccessDenied = (): LogoutIfAccessDenied => {
})
.catch(() => {});
}
const redirectTo =
e && e.redirectTo != null
? e.redirectTo
: error && error.redirectTo
? error.redirectTo
: undefined;

if (logoutUser) {
logout({}, redirectTo);
} else {
navigate(redirectTo);
if (redirectTo.startsWith('http')) {
// absolute link (e.g. https://my.oidc.server/login)
window.location.href = redirectTo;
} else {
// internal location
navigate(redirectTo);
}
}

return true;
Expand Down