mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-17 19:37:02 +00:00
improvement: keep user data on login fails
This commit is contained in:
@@ -63,9 +63,8 @@ const useAuth = () => {
|
||||
setLoading(false);
|
||||
}, []);
|
||||
|
||||
const handleLogin = async (e, user) => {
|
||||
const handleLogin = async user => {
|
||||
setLoading(true);
|
||||
e.preventDefault();
|
||||
|
||||
try {
|
||||
const { data } = await api.post("/auth/login", user);
|
||||
@@ -78,11 +77,11 @@ const useAuth = () => {
|
||||
setIsAuth(true);
|
||||
toast.success(i18n.t("auth.toasts.success"));
|
||||
history.push("/tickets");
|
||||
setLoading(false);
|
||||
} catch (err) {
|
||||
toastError(err);
|
||||
setLoading(false);
|
||||
}
|
||||
|
||||
setLoading(false);
|
||||
};
|
||||
|
||||
const handleLogout = e => {
|
||||
|
||||
@@ -61,6 +61,11 @@ const Login = () => {
|
||||
setUser({ ...user, [e.target.name]: e.target.value });
|
||||
};
|
||||
|
||||
const handlSubmit = e => {
|
||||
e.preventDefault();
|
||||
handleLogin(user);
|
||||
};
|
||||
|
||||
return (
|
||||
<Container component="main" maxWidth="xs">
|
||||
<CssBaseline />
|
||||
@@ -71,11 +76,7 @@ const Login = () => {
|
||||
<Typography component="h1" variant="h5">
|
||||
{i18n.t("login.title")}
|
||||
</Typography>
|
||||
<form
|
||||
className={classes.form}
|
||||
noValidate
|
||||
onSubmit={e => handleLogin(e, user)}
|
||||
>
|
||||
<form className={classes.form} noValidate onSubmit={handlSubmit}>
|
||||
<TextField
|
||||
variant="outlined"
|
||||
margin="normal"
|
||||
|
||||
@@ -7,19 +7,30 @@ import BackdropLoading from "../components/BackdropLoading";
|
||||
const RouteWrapper = ({ component: Component, isPrivate = false, ...rest }) => {
|
||||
const { isAuth, loading } = useContext(AuthContext);
|
||||
|
||||
if (loading) return <BackdropLoading />;
|
||||
|
||||
if (!isAuth && isPrivate) {
|
||||
return (
|
||||
<Redirect to={{ pathname: "/login", state: { from: rest.location } }} />
|
||||
<>
|
||||
{loading && <BackdropLoading />}
|
||||
<Redirect to={{ pathname: "/login", state: { from: rest.location } }} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
if (isAuth && !isPrivate) {
|
||||
return <Redirect to={{ pathname: "/", state: { from: rest.location } }} />;
|
||||
return (
|
||||
<>
|
||||
{loading && <BackdropLoading />}
|
||||
<Redirect to={{ pathname: "/", state: { from: rest.location } }} />;
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
return <Route {...rest} component={Component} />;
|
||||
return (
|
||||
<>
|
||||
{loading && <BackdropLoading />}
|
||||
<Route {...rest} component={Component} />
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default RouteWrapper;
|
||||
|
||||
Reference in New Issue
Block a user