fix(auth): prevent logout audit from blocking redirect
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
const KEYCLOAK_LOGOUT_PATH = "/api/auth/keycloak-logout";
|
||||
|
||||
type CompleteLogoutOptions = {
|
||||
reportAudit: () => Promise<unknown>;
|
||||
clearLocalState: () => void;
|
||||
navigate: (path: string) => void;
|
||||
};
|
||||
|
||||
type LogoutAuditOptions = {
|
||||
endpoint: string;
|
||||
accessToken?: string | null;
|
||||
fetcher?: typeof fetch;
|
||||
};
|
||||
|
||||
export const reportLogoutAudit = ({
|
||||
endpoint,
|
||||
accessToken,
|
||||
fetcher = fetch,
|
||||
}: LogoutAuditOptions) => {
|
||||
const headers = new Headers({ "Content-Type": "application/json" });
|
||||
if (accessToken) {
|
||||
headers.set("Authorization", `Bearer ${accessToken}`);
|
||||
}
|
||||
|
||||
return fetcher(endpoint, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify({ event: "logout" }),
|
||||
keepalive: true,
|
||||
});
|
||||
};
|
||||
|
||||
export const completeLogout = async ({
|
||||
reportAudit,
|
||||
clearLocalState,
|
||||
navigate,
|
||||
}: CompleteLogoutOptions) => {
|
||||
try {
|
||||
void reportAudit().catch(() => undefined);
|
||||
} catch {
|
||||
// Synchronous reporting errors must not block logout either.
|
||||
}
|
||||
|
||||
clearLocalState();
|
||||
navigate(KEYCLOAK_LOGOUT_PATH);
|
||||
return { success: true as const };
|
||||
};
|
||||
Reference in New Issue
Block a user