Skip to content

Commit 90e53b9

Browse files
authored
Merge branch 'main' into enhancement/api-gateway
2 parents 7371211 + b112463 commit 90e53b9

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

frontend/app/auth/auth-context.tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,20 @@ import {
1212
} from "react";
1313

1414
interface AuthContextType {
15-
user: User | null;
16-
token: string | null;
15+
user: User | null | undefined;
16+
token: string | null | undefined;
1717
login: (email: string, password: string) => Promise<User | undefined>;
1818
logout: () => Promise<void>;
19+
isLoading: boolean;
1920
}
2021

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

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

2931
useEffect(() => {
@@ -32,7 +34,11 @@ const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
3234

3335
// Login using locally stored JWT token
3436
useEffect(() => {
37+
if (token !== undefined) {
38+
setIsLoading(false);
39+
}
3540
if (token) {
41+
setIsLoading(true);
3642
fetch(
3743
`${userServiceUri(window.location.hostname, AuthType.Public)}/auth/verify-token`,
3844
{
@@ -45,10 +51,12 @@ const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
4551
.then((res) => {
4652
res.json().then((result) => {
4753
setUser(result.data);
54+
setIsLoading(false);
4855
});
4956
})
5057
.catch((err) => {
5158
console.error(err);
59+
setIsLoading(false);
5260
});
5361
}
5462
}, [token]);
@@ -88,14 +96,15 @@ const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
8896
};
8997

9098
const logout = async () => {
91-
setUser(null);
92-
setToken(null);
93-
localStorage.removeItem("jwtToken");
9499
router.push("/");
100+
localStorage.removeItem("jwtToken");
101+
setUser(undefined);
102+
setToken(undefined);
103+
setIsLoading(true);
95104
};
96105

97106
return (
98-
<AuthContext.Provider value={{ user, token, login, logout }}>
107+
<AuthContext.Provider value={{ user, token, login, logout, isLoading }}>
99108
{children}
100109
</AuthContext.Provider>
101110
);

frontend/app/layout.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import "./globals.css";
44
import { ThemeProvider } from "@/components/theme-provider";
55
import { Toaster } from "@/components/ui/toaster";
66
import AuthProvider from "@/app/auth/auth-context";
7-
import AuthPageWrapper from "@/components/auth/auth-page-wrapper";
87
import { Navbar } from "@/components/navbar";
98

109
const geistSans = localFont({
@@ -41,7 +40,7 @@ export default function RootLayout({
4140
>
4241
<AuthProvider>
4342
<Navbar />
44-
<AuthPageWrapper>{children}</AuthPageWrapper>
43+
{children}
4544
</AuthProvider>
4645
<Toaster />
4746
</ThemeProvider>

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)