From cc9f8341782fd55ef61058198f6cb0c044cf85d2 Mon Sep 17 00:00:00 2001 From: Cristhian Zanforlin Lousa Date: Tue, 26 Sep 2023 10:21:04 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(authContext.tsx):=20update?= =?UTF-8?q?=20cookie=20names=20for=20access=20token=20and=20refresh=20toke?= =?UTF-8?q?n=20to=20improve=20clarity=20and=20consistency=20=F0=9F=90=9B?= =?UTF-8?q?=20fix(api.tsx):=20update=20cookie=20names=20for=20access=20tok?= =?UTF-8?q?en=20and=20refresh=20token=20to=20match=20changes=20in=20authCo?= =?UTF-8?q?ntext.tsx?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/frontend/src/contexts/authContext.tsx | 18 +++++++++--------- src/frontend/src/controllers/API/api.tsx | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/frontend/src/contexts/authContext.tsx b/src/frontend/src/contexts/authContext.tsx index c917b4ef9..2d62fed07 100644 --- a/src/frontend/src/contexts/authContext.tsx +++ b/src/frontend/src/contexts/authContext.tsx @@ -26,10 +26,10 @@ export const AuthContext = createContext(initialValue); export function AuthProvider({ children }): React.ReactElement { const cookies = new Cookies(); const [accessToken, setAccessToken] = useState( - cookies.get("access_token") + cookies.get("access_tkn_lflw") ); const [refreshToken, setRefreshToken] = useState( - cookies.get("refresh_token") + cookies.get("refresh_tkn_lflw") ); const [isAuthenticated, setIsAuthenticated] = useState(false); const [isAdmin, setIsAdmin] = useState(false); @@ -37,7 +37,7 @@ export function AuthProvider({ children }): React.ReactElement { const [autoLogin, setAutoLogin] = useState(false); const { setLoading } = useContext(alertContext); useEffect(() => { - const storedAccessToken = cookies.get("access_token"); + const storedAccessToken = cookies.get("access_tkn_lflw"); if (storedAccessToken) { setAccessToken(storedAccessToken); } @@ -74,23 +74,23 @@ export function AuthProvider({ children }): React.ReactElement { }, []); function getAuthentication() { - const storedRefreshToken = cookies.get("refresh_token"); - const storedAccess = cookies.get("access_token"); + const storedRefreshToken = cookies.get("refresh_tkn_lflw"); + const storedAccess = cookies.get("access_tkn_lflw"); const auth = storedAccess && storedRefreshToken ? true : false; return auth; } function login(newAccessToken: string, refreshToken: string) { - cookies.set("access_token", newAccessToken, { path: "/" }); - cookies.set("refresh_token", refreshToken, { path: "/" }); + cookies.set("access_tkn_lflw", newAccessToken, { path: "/" }); + cookies.set("refresh_tkn_lflw", refreshToken, { path: "/" }); setAccessToken(newAccessToken); setRefreshToken(refreshToken); setIsAuthenticated(true); } function logout() { - cookies.remove("access_token", { path: "/" }); - cookies.remove("refresh_token", { path: "/" }); + cookies.remove("access_tkn_lflw", { path: "/" }); + cookies.remove("refresh_tkn_lflw", { path: "/" }); setIsAdmin(false); setUserData(null); setAccessToken(null); diff --git a/src/frontend/src/controllers/API/api.tsx b/src/frontend/src/controllers/API/api.tsx index a456f0ae7..75be74fac 100644 --- a/src/frontend/src/controllers/API/api.tsx +++ b/src/frontend/src/controllers/API/api.tsx @@ -23,7 +23,7 @@ function ApiInterceptor() { (response) => response, async (error: AxiosError) => { if (error.response?.status === 401) { - const refreshToken = cookies.get("refresh_token"); + const refreshToken = cookies.get("refresh_tkn_lflw"); if (refreshToken && refreshToken !== "auto") { authenticationErrorCount = authenticationErrorCount + 1; if (authenticationErrorCount > 3) { @@ -41,7 +41,7 @@ function ApiInterceptor() { if (error?.config?.headers) { delete error.config.headers["Authorization"]; error.config.headers["Authorization"] = `Bearer ${cookies.get( - "access_token" + "access_tkn_lflw" )}`; const response = await axios.request(error.config); return response;