synapse-react-client
Version:
[](https://badge.fury.io/js/synapse-react-client) [](https://github.com/prettier/prettie
120 lines (119 loc) • 4 kB
JavaScript
import { bindOAuthProviderToAccount as k, oAuthRegisterAccountStep2 as C, oAuthSessionRequest as _, setAccessTokenCookie as P, getRootURL as v } from "../../synapse-client/SynapseClient.js";
import "@sage-bionetworks/synapse-client/generated/models/ErrorResponseCode";
import "@sage-bionetworks/synapse-client/generated/models/TwoFactorAuthErrorResponse";
import "@sage-bionetworks/synapse-client/util/SynapseClientError";
import "@sage-bionetworks/synapse-types";
import "../functions/EntityTypeUtils.js";
import { BackendDestinationEnum as m } from "../functions/getEndpoint.js";
import "lodash-es";
import "@sage-bionetworks/synapse-client/util/synapseClientFetch";
import { useMemo as L, useState as N, useEffect as y } from "react";
import { OAUTH2_PROVIDERS as r } from "../SynapseConstants.js";
import { useOneSageURL as D } from "./useOneSageURL.js";
const S = "oauth2_csrf_token";
function F(i) {
try {
return localStorage.getItem(i);
} catch (f) {
return console.warn(`Unable to read from localStorage: ${i}`, f), null;
}
}
function A(i) {
try {
localStorage.removeItem(i);
} catch (f) {
console.warn(`Unable to remove from localStorage: ${i}`, f);
}
}
function V(i = {
isInitializingSession: !0,
isAuthenticated: !1
}) {
const f = D("/register1"), {
onSignInComplete: O,
registerAccountUrl: E = f.toString(),
onError: c,
onTwoFactorAuthRequired: R,
onTwoFactorAuthResetTokenPresent: h,
isInitializingSession: p,
isAuthenticated: I
} = i, T = v(), w = new URL(
typeof window < "u" ? window.location.href : "http://localhost/"
), { searchParams: a } = w, u = a?.get("code"), e = a?.get("provider"), g = !!(a?.get("client_id") && a?.get("redirect_uri")), l = L(() => {
if (!g) {
const s = a?.get("state");
try {
return s ? JSON.parse(decodeURIComponent(s)) : null;
} catch (o) {
console.error(
`Error parsing state param:
`,
o,
`
Encoded value:
`,
s
);
}
}
return null;
}, [g, a]), [U, d] = N(!!(u && e));
return y(() => {
if (!p && u && e) {
if (!g) {
const o = F(
S
), n = l?.csrfToken ?? null;
if (!(typeof n == "string" && typeof o == "string" && n === o)) {
A(S), console.error(
"Invalid or missing OAuth CSRF token detected. Aborting OAuth flow."
), c && c("Invalid OAuth state. Please try signing in again."), d(!1);
return;
}
A(S);
}
const s = `${T}?provider=${e}`;
if (r.ORCID == e && I) {
const o = (n) => {
console.error("Error binding ORCiD to account: ", n), c && c(n.reason);
};
k(
e,
u,
s,
m.REPO_ENDPOINT
).then(O).catch(o).finally(() => d(!1));
} else if (r.GOOGLE == e || r.ORCID == e || r.ARCUS == e || r.SAGE_BIONETWORKS == e) {
const o = (t) => {
t && ("accessToken" in t ? P(t.accessToken).then(
O
) : (R && R(t), // The user logged in with OAuth while attempting to disable 2FA using an emailed signed token
l && l.twoFaResetToken && h && h(
t,
l.twoFaResetToken
)));
}, n = (t) => {
t.status === 404 && window.location.replace(E), console.error("Error with account login: ", t), c && c(t.reason);
};
(r.GOOGLE == e || r.ARCUS == e || r.SAGE_BIONETWORKS == e) && l?.registrationUsername ? C(
l.registrationUsername,
e,
u,
s,
m.REPO_ENDPOINT
).then(o).catch(n).finally(() => d(!1)) : _(
e,
u,
s,
m.REPO_ENDPOINT
).then(o).catch(n).finally(() => d(!1));
} else
console.warn("Unknown SSO Provider: ", e), d(!1);
}
}, [p]), { isLoading: U };
}
export {
S as CSRF_TOKEN_STORAGE_KEY,
V as default
};
//# sourceMappingURL=useDetectSSOCode.js.map