diff options
| author | Andrew Lee <andrew@alee14.me> | 2025-03-25 17:23:30 -0400 |
|---|---|---|
| committer | Andrew Lee <andrew@alee14.me> | 2025-03-25 17:23:30 -0400 |
| commit | 2c783bdb703e4ad69c9f3f846c2c9e6a527ccc80 (patch) | |
| tree | e02587b4dca4bd7027b4cc012d458b18392643ea /web/src/context | |
| parent | 1c12d378d66b92b1674acd17640f2bac752da289 (diff) | |
| download | AleeBot-2c783bdb703e4ad69c9f3f846c2c9e6a527ccc80.tar.gz AleeBot-2c783bdb703e4ad69c9f3f846c2c9e6a527ccc80.tar.bz2 AleeBot-2c783bdb703e4ad69c9f3f846c2c9e6a527ccc80.zip | |
Implemented admin dashboard
Diffstat (limited to 'web/src/context')
| -rw-r--r-- | web/src/context/middleware.js | 37 |
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); |
