synapse-react-client
Version:
[](https://badge.fury.io/js/synapse-react-client) [](https://github.com/prettier/prettie
81 lines (80 loc) • 2.28 kB
JavaScript
import { useTermsOfServiceStatus as E } from "../../../synapse-queries/termsOfService/useTermsOfService.js";
import { useGetTwoFactorEnrollmentStatusWithAccessToken as I } from "../../../synapse-queries/auth/useTwoFactorEnrollment.js";
import { useState as d, useRef as O, useEffect as h, useSyncExternalStore as T, useCallback as m } from "react";
import { useNavigate as x } from "react-router";
import F from "../../hooks/useDetectSSOCode.js";
import { restoreLastPlace as v } from "../AppUtils.js";
import { SynapseSessionManager as R } from "./SynapseSessionManager.js";
function D(f = {}) {
const {
defaultRealm: o = "0",
maxAge: n,
onMissingExpectedAuthentication: r,
onTwoFactorAuthResetThroughSSO: c
} = f, i = x(), [l, a] = d(), u = O(
r
);
u.current = r;
const [t] = d(
() => new R({
defaultRealm: o,
maxAge: n,
onMissingExpectedAuthentication: () => {
u.current?.();
}
})
);
h(() => {
t.setOptions({ defaultRealm: o, maxAge: n });
}, [t, o, n]);
const e = T(
t.subscribe,
t.getSnapshot,
t.getServerSnapshot
);
h(() => (t.start(), () => t.dispose()), [t]);
const { data: p } = E(e.token, {
enabled: e.isAuthenticated
}), { data: g } = I(e.token, {
enabled: e.isAuthenticated
}), S = m(async () => {
a(void 0), await t.refreshSession();
}, [t]), A = m(
async (s) => {
await t.clearSession(), s && s(), i(0);
},
[i, t]
), { isLoading: w } = F({
onSignInComplete: () => {
v(i), S();
},
onTwoFactorAuthRequired: (s) => {
a(s);
},
onTwoFactorAuthResetTokenPresent: (s, k) => {
a(s), c && c(s, k);
},
onError: (s) => {
throw s;
},
isInitializingSession: !e.hasInitializedSession,
isAuthenticated: e.isAuthenticated
});
return { sessionContext: {
token: e.token,
userId: e.userId,
realmId: e.realmId,
isAuthenticated: e.isAuthenticated,
termsOfServiceStatus: p,
refreshSession: S,
clearSession: A,
isLoadingSSO: w,
twoFactorAuthSSOErrorResponse: l,
hasInitializedSession: e.hasInitializedSession,
twoFactorStatus: g
}, token: e.token };
}
export {
D as useSessionManager
};
//# sourceMappingURL=useSessionManager.js.map