From 8733795c8449f3514369d7b4220934760e386f1b Mon Sep 17 00:00:00 2001
From: Biswa Kalyan Bhuyan <biswa@surgot.in>
Date: Fri, 25 Apr 2025 01:09:30 +0530
Subject: finance/frontend: fix: did some minor changes to the frontend

---
 frontend/src/components/shared/AuthContext.tsx | 92 ++++++++++++++++++++++++++
 1 file changed, 92 insertions(+)
 create mode 100644 frontend/src/components/shared/AuthContext.tsx

(limited to 'frontend/src/components/shared/AuthContext.tsx')

diff --git a/frontend/src/components/shared/AuthContext.tsx b/frontend/src/components/shared/AuthContext.tsx
new file mode 100644
index 0000000..a3ec5a0
--- /dev/null
+++ b/frontend/src/components/shared/AuthContext.tsx
@@ -0,0 +1,92 @@
+'use client';
+
+import { createContext, useContext, useEffect, useState, ReactNode } from 'react';
+import { useRouter } from 'next/navigation';
+import { authApi, userApi } from '@/lib/api';
+
+interface User {
+  ID: number;
+  Name: string;
+  Email: string;
+}
+
+interface AuthContextType {
+  user: User | null;
+  isLoading: boolean;
+  isAuthenticated: boolean;
+  login: (email: string, password: string) => Promise<void>;
+  signup: (name: string, email: string, password: string) => Promise<void>;
+  logout: () => void;
+}
+
+const AuthContext = createContext<AuthContextType | null>(null);
+
+export function AuthProvider({ children }: { children: ReactNode }) {
+  const [user, setUser] = useState<User | null>(null);
+  const [isLoading, setIsLoading] = useState<boolean>(true);
+  const router = useRouter();
+  
+  // Check if the user is already logged in
+  useEffect(() => {
+    const checkAuth = async () => {
+      const token = localStorage.getItem('token');
+      if (!token) {
+        setIsLoading(false);
+        return;
+      }
+      
+      try {
+        const userData = await userApi.getProfile();
+        setUser(userData);
+      } catch (error) {
+        // Clear invalid token
+        localStorage.removeItem('token');
+      } finally {
+        setIsLoading(false);
+      }
+    };
+    
+    checkAuth();
+  }, []);
+  
+  // Login function
+  const login = async (email: string, password: string) => {
+    const response = await authApi.login(email, password);
+    setUser(response.user);
+    return response;
+  };
+  
+  // Signup function
+  const signup = async (name: string, email: string, password: string) => {
+    return await authApi.signup(name, email, password);
+  };
+  
+  // Logout function
+  const logout = () => {
+    authApi.logout();
+    setUser(null);
+  };
+  
+  return (
+    <AuthContext.Provider 
+      value={{ 
+        user, 
+        isLoading, 
+        isAuthenticated: !!user, 
+        login, 
+        signup, 
+        logout 
+      }}
+    >
+      {children}
+    </AuthContext.Provider>
+  );
+}
+
+export function useAuth() {
+  const context = useContext(AuthContext);
+  if (!context) {
+    throw new Error('useAuth must be used within an AuthProvider');
+  }
+  return context;
+} 
\ No newline at end of file
-- 
cgit v1.2.3-59-g8ed1b

