Files
workavia-calendar-front/src/features/User/LoginCallback.tsx
T
Camille Moussu 6839353f34 [#418] improved retry logic to prevent loading loop (#427)
* [#418] improved retry logic to prevent loading loop

* [#418] added FullJitter algorithm

Co-authored-by: Benoit TELLIER <btellier@linagora.com>
2025-12-19 12:24:34 +01:00

67 lines
2.1 KiB
TypeScript

import { useEffect, useRef } from "react";
import { Callback } from "./oidcAuth";
import { useAppDispatch } from "../../app/hooks";
import { push } from "redux-first-history";
import {
getOpenPaasUserDataAsync,
setTokens,
setUserData,
setUserError,
} from "./userSlice";
import { Loading } from "../../components/Loading/Loading";
import { getCalendarsListAsync } from "../Calendars/CalendarSlice";
export function CallbackResume() {
const dispatch = useAppDispatch();
const hasRun = useRef(false);
const calendarsLoadingRef = useRef(false);
const saved = sessionStorage.getItem("redirectState")
? JSON.parse(sessionStorage.getItem("redirectState")!)
: null;
useEffect(() => {
if (hasRun.current) {
return;
}
hasRun.current = true;
const runCallback = async () => {
try {
const data = await Callback(saved?.code_verifier, saved?.state);
if (!data || !data.userinfo || !data.tokenSet) {
throw new Error("OAuth callback failed");
}
dispatch(setUserData(data?.userinfo));
dispatch(setTokens(data?.tokenSet));
await dispatch(getOpenPaasUserDataAsync());
if (!calendarsLoadingRef.current) {
calendarsLoadingRef.current = true;
await dispatch(getCalendarsListAsync());
calendarsLoadingRef.current = false;
}
sessionStorage.removeItem("redirectState");
sessionStorage.setItem("tokenSet", JSON.stringify(data?.tokenSet));
sessionStorage.setItem("userData", JSON.stringify(data?.userinfo));
// Redirect to main page after successful callback
dispatch(push("/"));
} catch (e) {
console.error("OIDC callback error:", e);
// Redirect to error page after error
dispatch(
setUserError(e instanceof Error ? e.message : "OAuth callback failed")
);
dispatch(push("/error"));
}
};
if (saved?.code_verifier) {
runCallback();
} else {
console.warn("Missing redirectState");
sessionStorage.removeItem("redirectState");
dispatch(push("/"));
}
}, [dispatch, saved]);
return <Loading />;
}