aboutsummaryrefslogtreecommitdiff
path: root/web/src/context
diff options
context:
space:
mode:
Diffstat (limited to 'web/src/context')
-rw-r--r--web/src/context/middleware.js37
1 files changed, 37 insertions, 0 deletions
diff --git a/web/src/context/middleware.js b/web/src/context/middleware.js
new file mode 100644
index 0000000..044a6b5
--- /dev/null
+++ b/web/src/context/middleware.js
@@ -0,0 +1,37 @@
+'use client';
+import { createContext, useContext, useEffect, useState } from 'react';
+import { useRouter } from 'next/navigation';
+
+const AuthContext = createContext();
+
+export function AuthProvider({ children }) {
+ const [isAuthenticated, setIsAuthenticated] = useState(false);
+ const [isLoading, setIsLoading] = useState(true);
+ const router = useRouter();
+
+ useEffect(() => {
+ // Check if token exists
+ const token = localStorage.getItem('token');
+ if (token) {
+ setIsAuthenticated(true);
+ } else {
+ router.push('/');
+ }
+ setIsLoading(false);
+ }, [router]);
+
+ const logout = () => {
+ localStorage.removeItem('token');
+ localStorage.removeItem('apiUrl');
+ setIsAuthenticated(false);
+ router.push('/');
+ };
+
+ return (
+ <AuthContext.Provider value={{ isAuthenticated, isLoading, logout }}>
+ {children}
+ </AuthContext.Provider>
+ );
+}
+
+export const useAuth = () => useContext(AuthContext);