Switch account when adding another account
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
6456737d8a
commit
bb2a0002a6
|
@ -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();
|
||||||
};
|
};
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue