Skip to content

Commit

Permalink
fix: token expire
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-vm committed Mar 4, 2024
1 parent cf7c215 commit bd17009
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 23 deletions.
22 changes: 5 additions & 17 deletions frontend/src/context/authentication/authentication.context.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Loading from "components/Loading";
import React, {
type ReactNode,
useCallback,
Expand All @@ -13,7 +12,6 @@ import { createCustomContext } from "utils";

import { postAuthLogin, postAuthRegister } from "./api";
import {
authStatus,
type IAuthenticationContext,
type IAuthenticationStore,
} from "./authentication.interface";
Expand All @@ -30,7 +28,6 @@ export const AuthenticationProvider: React.FC<{ children: ReactNode }> = ({
children,
}) => {
const navigate = useNavigate();
const [status, setStatus] = useState(authStatus.Loading);
const [authLoading, setAuthLoading] = useState(false);
const [store, setStore] = useState<IAuthenticationStore>({
token: localStorage.getItem("auth_token"),
Expand Down Expand Up @@ -58,7 +55,6 @@ export const AuthenticationProvider: React.FC<{ children: ReactNode }> = ({
"tokenExpiresAtTimestamp",
tokenExpirationDate.getTime().toString(),
);
setStatus(authStatus.SignedIn);
navigate(redirect);
},
[navigate],
Expand All @@ -71,7 +67,6 @@ export const AuthenticationProvider: React.FC<{ children: ReactNode }> = ({
...store,
token: null,
}));
setStatus(authStatus.SignedOut);
navigate("/sign-in");
}, [navigate]);

Expand Down Expand Up @@ -123,9 +118,9 @@ export const AuthenticationProvider: React.FC<{ children: ReactNode }> = ({
const tokenExpired = useCallback(() => {
const tokenTimestamp = localStorage.getItem("tokenExpiresAtTimestamp");
if (tokenTimestamp) {
const date1 = Number(tokenTimestamp);
const date2 = new Date().getTime();
return date1 <= date2;
const expireDate = Number(tokenTimestamp);
const currentDate = new Date().getTime();
return expireDate <= currentDate;
}
return true;
}, []);
Expand All @@ -146,13 +141,10 @@ export const AuthenticationProvider: React.FC<{ children: ReactNode }> = ({

useEffect(() => {
const expired = tokenExpired();

if (expired) {
if (expired && isLogged.current) {
logout();
} else {
setStatus(authStatus.SignedIn);
}
}, [tokenExpired]);
}, [tokenExpired, isLogged]);

const value = useMemo((): IAuthenticationContext => {
return {
Expand All @@ -165,10 +157,6 @@ export const AuthenticationProvider: React.FC<{ children: ReactNode }> = ({
};
}, [store, logout, authenticate, register, authLoading]);

if (status === authStatus.Loading) {
return <Loading />;
}

return (
<AuthenticationContext.Provider value={value}>
{children}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ export interface IAuthenticationStore {
userId: string | null;
}

export enum authStatus {
Loading,
SignedIn,
SignedOut,
}

export interface IAuthenticationContext {
store: IAuthenticationStore;
isLogged: boolean;
Expand Down

0 comments on commit bd17009

Please sign in to comment.