mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 20:59:16 +00:00
fix: error in session handler after login / create account
This commit is contained in:
@@ -12,6 +12,13 @@ const useAuth = () => {
|
|||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (
|
||||||
|
history.location.pathname === "/login" ||
|
||||||
|
history.location.pathname === "/signup"
|
||||||
|
) {
|
||||||
|
setLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
const token = localStorage.getItem("token");
|
const token = localStorage.getItem("token");
|
||||||
if (token) {
|
if (token) {
|
||||||
api.defaults.headers.Authorization = `Bearer ${JSON.parse(token)}`;
|
api.defaults.headers.Authorization = `Bearer ${JSON.parse(token)}`;
|
||||||
@@ -61,10 +68,10 @@ const useAuth = () => {
|
|||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}, []);
|
}, [history]);
|
||||||
|
|
||||||
const handleLogin = async (e, user) => {
|
const handleLogin = async (e, user) => {
|
||||||
// setLoading(true);
|
setLoading(true);
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
try {
|
try {
|
||||||
const { data } = await api.post("/auth/login", user);
|
const { data } = await api.post("/auth/login", user);
|
||||||
@@ -75,18 +82,19 @@ const useAuth = () => {
|
|||||||
api.defaults.headers.Authorization = `Bearer ${data.token}`;
|
api.defaults.headers.Authorization = `Bearer ${data.token}`;
|
||||||
setIsAuth(true);
|
setIsAuth(true);
|
||||||
toast.success(i18n.t("auth.toasts.success"));
|
toast.success(i18n.t("auth.toasts.success"));
|
||||||
// setLoading(false);
|
setLoading(false);
|
||||||
history.push("/tickets");
|
history.push("/tickets");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
if (err.response && err.response.data && err.response.data.error) {
|
if (err.response && err.response.data && err.response.data.error) {
|
||||||
toast.error(err.response.data.error);
|
toast.error(err.response.data.error);
|
||||||
}
|
}
|
||||||
|
setLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleLogout = e => {
|
const handleLogout = e => {
|
||||||
// setLoading(true);
|
setLoading(true);
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
setIsAuth(false);
|
setIsAuth(false);
|
||||||
localStorage.removeItem("token");
|
localStorage.removeItem("token");
|
||||||
@@ -94,7 +102,7 @@ const useAuth = () => {
|
|||||||
localStorage.removeItem("profile");
|
localStorage.removeItem("profile");
|
||||||
localStorage.removeItem("userId");
|
localStorage.removeItem("userId");
|
||||||
api.defaults.headers.Authorization = undefined;
|
api.defaults.headers.Authorization = undefined;
|
||||||
// setLoading(false);
|
setLoading(false);
|
||||||
history.push("/login");
|
history.push("/login");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user