🐛 fix(authContext.tsx): update cookie names for access token and refresh token to improve clarity and consistency
🐛 fix(api.tsx): update cookie names for access token and refresh token to match changes in authContext.tsx
This commit is contained in:
parent
5c57e62f55
commit
cc9f834178
2 changed files with 11 additions and 11 deletions
|
|
@ -26,10 +26,10 @@ export const AuthContext = createContext<AuthContextType>(initialValue);
|
||||||
export function AuthProvider({ children }): React.ReactElement {
|
export function AuthProvider({ children }): React.ReactElement {
|
||||||
const cookies = new Cookies();
|
const cookies = new Cookies();
|
||||||
const [accessToken, setAccessToken] = useState<string | null>(
|
const [accessToken, setAccessToken] = useState<string | null>(
|
||||||
cookies.get("access_token")
|
cookies.get("access_tkn_lflw")
|
||||||
);
|
);
|
||||||
const [refreshToken, setRefreshToken] = useState<string | null>(
|
const [refreshToken, setRefreshToken] = useState<string | null>(
|
||||||
cookies.get("refresh_token")
|
cookies.get("refresh_tkn_lflw")
|
||||||
);
|
);
|
||||||
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(false);
|
const [isAuthenticated, setIsAuthenticated] = useState<boolean>(false);
|
||||||
const [isAdmin, setIsAdmin] = useState<boolean>(false);
|
const [isAdmin, setIsAdmin] = useState<boolean>(false);
|
||||||
|
|
@ -37,7 +37,7 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
const [autoLogin, setAutoLogin] = useState<boolean>(false);
|
const [autoLogin, setAutoLogin] = useState<boolean>(false);
|
||||||
const { setLoading } = useContext(alertContext);
|
const { setLoading } = useContext(alertContext);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const storedAccessToken = cookies.get("access_token");
|
const storedAccessToken = cookies.get("access_tkn_lflw");
|
||||||
if (storedAccessToken) {
|
if (storedAccessToken) {
|
||||||
setAccessToken(storedAccessToken);
|
setAccessToken(storedAccessToken);
|
||||||
}
|
}
|
||||||
|
|
@ -74,23 +74,23 @@ export function AuthProvider({ children }): React.ReactElement {
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
function getAuthentication() {
|
function getAuthentication() {
|
||||||
const storedRefreshToken = cookies.get("refresh_token");
|
const storedRefreshToken = cookies.get("refresh_tkn_lflw");
|
||||||
const storedAccess = cookies.get("access_token");
|
const storedAccess = cookies.get("access_tkn_lflw");
|
||||||
const auth = storedAccess && storedRefreshToken ? true : false;
|
const auth = storedAccess && storedRefreshToken ? true : false;
|
||||||
return auth;
|
return auth;
|
||||||
}
|
}
|
||||||
|
|
||||||
function login(newAccessToken: string, refreshToken: string) {
|
function login(newAccessToken: string, refreshToken: string) {
|
||||||
cookies.set("access_token", newAccessToken, { path: "/" });
|
cookies.set("access_tkn_lflw", newAccessToken, { path: "/" });
|
||||||
cookies.set("refresh_token", refreshToken, { path: "/" });
|
cookies.set("refresh_tkn_lflw", refreshToken, { path: "/" });
|
||||||
setAccessToken(newAccessToken);
|
setAccessToken(newAccessToken);
|
||||||
setRefreshToken(refreshToken);
|
setRefreshToken(refreshToken);
|
||||||
setIsAuthenticated(true);
|
setIsAuthenticated(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
function logout() {
|
function logout() {
|
||||||
cookies.remove("access_token", { path: "/" });
|
cookies.remove("access_tkn_lflw", { path: "/" });
|
||||||
cookies.remove("refresh_token", { path: "/" });
|
cookies.remove("refresh_tkn_lflw", { path: "/" });
|
||||||
setIsAdmin(false);
|
setIsAdmin(false);
|
||||||
setUserData(null);
|
setUserData(null);
|
||||||
setAccessToken(null);
|
setAccessToken(null);
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ function ApiInterceptor() {
|
||||||
(response) => response,
|
(response) => response,
|
||||||
async (error: AxiosError) => {
|
async (error: AxiosError) => {
|
||||||
if (error.response?.status === 401) {
|
if (error.response?.status === 401) {
|
||||||
const refreshToken = cookies.get("refresh_token");
|
const refreshToken = cookies.get("refresh_tkn_lflw");
|
||||||
if (refreshToken && refreshToken !== "auto") {
|
if (refreshToken && refreshToken !== "auto") {
|
||||||
authenticationErrorCount = authenticationErrorCount + 1;
|
authenticationErrorCount = authenticationErrorCount + 1;
|
||||||
if (authenticationErrorCount > 3) {
|
if (authenticationErrorCount > 3) {
|
||||||
|
|
@ -41,7 +41,7 @@ function ApiInterceptor() {
|
||||||
if (error?.config?.headers) {
|
if (error?.config?.headers) {
|
||||||
delete error.config.headers["Authorization"];
|
delete error.config.headers["Authorization"];
|
||||||
error.config.headers["Authorization"] = `Bearer ${cookies.get(
|
error.config.headers["Authorization"] = `Bearer ${cookies.get(
|
||||||
"access_token"
|
"access_tkn_lflw"
|
||||||
)}`;
|
)}`;
|
||||||
const response = await axios.request(error.config);
|
const response = await axios.request(error.config);
|
||||||
return response;
|
return response;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue