Skip to content

Commit fe56030

Browse files
committed
Fix logout bug
1 parent cdb7119 commit fe56030

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

frontend/app/auth/auth-context.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@ import {
1111
} from "react";
1212

1313
interface AuthContextType {
14-
user: User | null;
15-
token: string | null;
14+
user: User | null | undefined;
15+
token: string | null | undefined;
1616
login: (email: string, password: string) => Promise<User | undefined>;
1717
logout: () => Promise<void>;
18+
isLoading: boolean;
1819
}
1920

2021
const AuthContext = createContext<AuthContextType | undefined>(undefined);
2122

2223
const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
2324
const tokenKey = "jwtToken";
24-
const [user, setUser] = useState<User | null>(null);
25-
const [token, setToken] = useState<string | null>(null);
25+
const [user, setUser] = useState<User | null>();
26+
const [token, setToken] = useState<string | null>();
27+
const [isLoading, setIsLoading] = useState<boolean>(true);
2628
const router = useRouter();
2729

2830
useEffect(() => {
@@ -31,7 +33,11 @@ const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
3133

3234
// Login using locally stored JWT token
3335
useEffect(() => {
36+
if (token !== undefined) {
37+
setIsLoading(false);
38+
}
3439
if (token) {
40+
setIsLoading(true);
3541
fetch("http://localhost:3001/auth/verify-token", {
3642
method: "GET",
3743
headers: {
@@ -41,10 +47,12 @@ const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
4147
.then((res) => {
4248
res.json().then((result) => {
4349
setUser(result.data);
50+
setIsLoading(false);
4451
});
4552
})
4653
.catch((err) => {
4754
console.error(err);
55+
setIsLoading(false);
4856
});
4957
}
5058
}, [token]);
@@ -88,7 +96,7 @@ const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
8896
};
8997

9098
return (
91-
<AuthContext.Provider value={{ user, token, login, logout }}>
99+
<AuthContext.Provider value={{ user, token, login, logout, isLoading }}>
92100
{children}
93101
</AuthContext.Provider>
94102
);

frontend/components/auth/auth-page-wrapper.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ReactNode } from "react";
55
import { useAuth } from "@/app/auth/auth-context";
66
import { Button } from "@/components/ui/button";
77
import { useRouter } from "next/navigation";
8+
import LoadingScreen from "../common/loading-screen";
89

910
type AuthCheck = (
1011
user: { id: string; isAdmin: boolean } | undefined | null
@@ -49,7 +50,9 @@ const AuthPageWrapper: React.FC<AuthPageWrapperProps> = ({
4950

5051
return (
5152
<div>
52-
{authCheck(auth?.user) ? (
53+
{auth?.isLoading ? (
54+
<LoadingScreen />
55+
) : authCheck(auth?.user) ? (
5356
children
5457
) : (
5558
<div className="flex items-start justify-center h-2/6">

0 commit comments

Comments
 (0)