synapse-react-client
Version:
[](https://badge.fury.io/js/synapse-react-client) [](https://github.com/prettier/prettie
167 lines (166 loc) • 5.25 kB
JavaScript
import * as u from "../../synapse-client/SynapseClient.js";
import "@sage-bionetworks/synapse-client/generated/models/ErrorResponseCode";
import { instanceOfTwoFactorAuthErrorResponse as y } from "@sage-bionetworks/synapse-client/generated/models/TwoFactorAuthErrorResponse";
import "@sage-bionetworks/synapse-client/util/SynapseClientError";
import { ErrorResponseCode as I } from "@sage-bionetworks/synapse-types";
import "../functions/EntityTypeUtils.js";
import { AUTHENTICATION_RECEIPT_LOCALSTORAGE_KEY as R } from "../SynapseConstants.js";
import { noop as g } from "lodash-es";
import "@sage-bionetworks/synapse-client/util/synapseClientFetch";
import { useMutation as l } from "@tanstack/react-query";
import "../PermissionLevelToAccessType.js";
import { useState as d, useEffect as a } from "react";
import { useOneSageURL as N } from "./useOneSageURL.js";
import "use-deep-compare-effect";
import "../context/SynapseContext.js";
import "react/jsx-runtime";
import "@mui/material";
import "./useCookiePreferences.js";
import "./useSourceAppConfigs.js";
import "universal-cookie";
import "../AppUtils/session/ApplicationSessionContext.js";
import "../context/FullContextProvider.js";
import "../context/DocumentMetadataContext.js";
import '../../style/components/_spinner.css';/* empty css */
import { useResetTwoFactorAuth as V } from "../../synapse-queries/auth/useTwoFactorEnrollment.js";
import "react-router";
import "@sage-bionetworks/synapse-client";
import "../types/IsType.js";
import "@tanstack/query-core";
import "lodash-es/isEmpty";
import "lodash-es/isEqual";
import "lodash-es/xorWith";
const F = [
"VERIFICATION_CODE",
"RECOVERY_CODE",
"LOGGED_IN",
"DISABLE_2FA_PROMPT"
];
function fo(h) {
const {
sessionCallback: p = g,
twoFaErrorResponse: m,
onTwoFactorAuthRequired: w = g
} = h, [s, i] = d("CHOOSE_AUTH_METHOD"), [A, r] = d(), [e, c] = d(), O = N("/changePassword");
a(() => {
m && c(m);
}, [m]), a(() => {
const o = new URL(window.location.href), { searchParams: t } = o;
if (t) {
const n = t.get("userId"), f = t.get("twoFaToken");
n && f && (c({
errorCode: I.TWO_FA_REQUIRED,
reason: "",
userId: parseInt(n, 10),
twoFaToken: f,
concreteType: "org.sagebionetworks.repo.model.auth.TwoFactorAuthErrorResponse"
}), F.includes(s) || i("VERIFICATION_CODE"));
}
}, [s]), a(() => {
e && (c(e), F.includes(s) || i("VERIFICATION_CODE"));
}, [e]), a(() => {
e && w(e);
}, [w, e]), a(() => {
r(void 0);
}, [s]);
async function E(o) {
await u.setAccessTokenCookie(o.accessToken), localStorage.setItem(
R,
o.authenticationReceipt ?? ""
), i("LOGGED_IN"), p && p();
}
const {
mutate: C,
isPending: _
} = l({
mutationFn: ({ username: o, password: t, authenticationReceipt: n }) => u.login(o, t, n),
onError: (o) => {
r(o.reason);
const { errorResponse: t } = o;
t && "errorCode" in t && t.errorCode == I.PASSWORD_RESET_VIA_EMAIL_REQUIRED && window.location.assign(O.toString());
},
onSuccess: async (o) => {
o && (y(o) ? (i("VERIFICATION_CODE"), c(o)) : await E(o));
}
}), {
mutate: P,
isPending: S
} = l(
{
mutationFn: u.loginWith2fa,
onError: (o) => {
r(o.reason), // The twoFaToken wasn't transmitted correctly
(o.reason.includes("The provided twoFaToken is invalid") || // The user waited too long to enter the code.
o.reason.includes("Token has expired")) && (console.warn(o), r(
"Something went wrong. Refresh the page and try again."
), window.location.href.includes("twoFaToken") && window.history.replaceState(
{},
document.title,
// using regex because SWC hashbang doesn't work with URLSearchParams
window.location.href.replaceAll(
/(twoFaToken|userId)=[^&]*&?/g,
""
)
));
},
onSuccess: E
}
), {
mutate: D,
isSuccess: k,
isPending: L
} = V({
onError: (o) => {
r(o.reason);
}
}), U = (o, t) => {
r(void 0);
const n = localStorage.getItem(
R
);
C({
username: o,
password: t,
authenticationReceipt: n
});
};
function T(o) {
return o == null ? (r(
"You did not first log in with your password or a third-party identity provider."
), !1) : !0;
}
return {
step: s,
onStepChange: i,
submitUsernameAndPassword: U,
submitOneTimePassword: (o, t = s === "RECOVERY_CODE" ? "RECOVERY_CODE" : "TOTP") => {
if (r(void 0), T(e)) {
const n = {
userId: e.userId,
twoFaToken: e.twoFaToken,
otpCode: o,
otpType: t
};
P(n);
}
},
errorMessage: A,
loginIsPending: _ || S,
beginTwoFactorAuthReset: (o) => {
if (r(void 0), T(e)) {
const t = {
userId: e.userId,
twoFaToken: e.twoFaToken,
twoFaResetEndpoint: o
};
D(t);
}
},
twoFactorAuthResetIsPending: L,
twoFactorAuthResetIsSuccess: k
};
}
export {
fo as default
};
//# sourceMappingURL=useLogin.js.map