Merge branch 'add-account-fix' into 'develop'

Switch account when adding another account

See merge request soapbox-pub/soapbox!1935
modal-video-fix
marcin mikołajczak 2022-11-25 15:53:09 +00:00
commit 979b7c3446
2 zmienionych plików z 30 dodań i 29 usunięć

Wyświetl plik

@ -26,32 +26,35 @@ const LoginPage = () => {
const [mfaToken, setMfaToken] = useState(token || ''); const [mfaToken, setMfaToken] = useState(token || '');
const [shouldRedirect, setShouldRedirect] = useState(false); const [shouldRedirect, setShouldRedirect] = useState(false);
const getFormData = (form: HTMLFormElement) => { const getFormData = (form: HTMLFormElement) =>
return Object.fromEntries( Object.fromEntries(
Array.from(form).map((i: any) => [i.name, i.value]), Array.from(form).map((i: any) => [i.name, i.value]),
); );
};
const handleSubmit: React.FormEventHandler = (event) => { const handleSubmit: React.FormEventHandler = (event) => {
const { username, password } = getFormData(event.target as HTMLFormElement); const { username, password } = getFormData(event.target as HTMLFormElement);
dispatch(logIn(username, password)).then(({ access_token }) => { dispatch(logIn(username, password))
return dispatch(verifyCredentials(access_token as string)) .then(({ access_token }) => dispatch(verifyCredentials(access_token as string)))
// Refetch the instance for authenticated fetch // Refetch the instance for authenticated fetch
.then(() => dispatch(fetchInstance() as any)); .then(async (account) => {
}).then((account: { id: string }) => { await dispatch(fetchInstance());
dispatch(closeModal()); return account;
setShouldRedirect(true); })
if (typeof me === 'string') { .then((account: { id: string }) => {
dispatch(switchAccount(account.id)); dispatch(closeModal());
} if (typeof me === 'string') {
}).catch((error: AxiosError) => { dispatch(switchAccount(account.id));
const data: any = error.response?.data; } else {
if (data?.error === 'mfa_required') { setShouldRedirect(true);
setMfaAuthNeeded(true); }
setMfaToken(data.mfa_token); }).catch((error: AxiosError) => {
} const data: any = error.response?.data;
setIsLoading(false); if (data?.error === 'mfa_required') {
}); setMfaAuthNeeded(true);
setMfaToken(data.mfa_token);
}
setIsLoading(false);
});
setIsLoading(true); setIsLoading(true);
event.preventDefault(); event.preventDefault();
}; };

Wyświetl plik

@ -50,14 +50,12 @@ const Header = () => {
setLoading(true); setLoading(true);
dispatch(logIn(username, password) as any) dispatch(logIn(username, password) as any)
.then(({ access_token }: { access_token: string }) => { .then(({ access_token }: { access_token: string }) => (
return ( dispatch(verifyCredentials(access_token) as any)
dispatch(verifyCredentials(access_token) as any) // Refetch the instance for authenticated fetch
// Refetch the instance for authenticated fetch .then(() => dispatch(fetchInstance()))
.then(() => dispatch(fetchInstance())) .then(() => setShouldRedirect(true))
.then(() => setShouldRedirect(true)) ))
);
})
.catch((error: AxiosError) => { .catch((error: AxiosError) => {
setLoading(false); setLoading(false);