fix: loggin out keeps refresh token in browser

fix: https://github.com/canove/whaticket/issues/106
This commit is contained in:
canove
2021-01-15 11:56:28 -03:00
parent e6e9ac213f
commit 74e17a9f04
8 changed files with 59 additions and 29 deletions

View File

@@ -1,6 +1,6 @@
import React, { createContext } from "react";
import useAuth from "./useAuth";
import useAuth from "../../hooks/useAuth.js";
const AuthContext = createContext();

View File

@@ -1,6 +1,6 @@
import React, { createContext } from "react";
import useWhatsApps from "./useWhatsApps";
import useWhatsApps from "../../hooks/useWhatsApps";
const WhatsAppsContext = createContext();

View File

@@ -102,14 +102,21 @@ const useAuth = () => {
}
};
const handleLogout = () => {
const handleLogout = async () => {
setLoading(true);
setIsAuth(false);
setUser({});
localStorage.removeItem("token");
api.defaults.headers.Authorization = undefined;
setLoading(false);
history.push("/login");
try {
await api.delete("/auth/logout");
setIsAuth(false);
setUser({});
localStorage.removeItem("token");
api.defaults.headers.Authorization = undefined;
setLoading(false);
history.push("/login");
} catch (err) {
toastError(err);
setLoading(false);
}
};
return { isAuth, user, loading, handleLogin, handleLogout };

View File

@@ -131,6 +131,11 @@ const LoggedInLayout = ({ children }) => {
handleCloseMenu();
};
const handleClickLogout = () => {
handleCloseMenu();
handleLogout();
};
if (loading) {
return <BackdropLoading />;
}
@@ -190,7 +195,7 @@ const LoggedInLayout = ({ children }) => {
>
WhaTicket
</Typography>
<NotificationsPopOver />
{user.id && <NotificationsPopOver />}
<div>
<IconButton
@@ -220,7 +225,7 @@ const LoggedInLayout = ({ children }) => {
<MenuItem onClick={handleOpenUserModal}>
{i18n.t("mainDrawer.appBar.user.profile")}
</MenuItem>
<MenuItem onClick={handleLogout}>
<MenuItem onClick={handleClickLogout}>
{i18n.t("mainDrawer.appBar.user.logout")}
</MenuItem>
</Menu>