diff --git a/src/context/AuthContext.tsx b/src/context/AuthContext.tsx index 284c4ef..3573831 100644 --- a/src/context/AuthContext.tsx +++ b/src/context/AuthContext.tsx @@ -1,86 +1,75 @@ import React, { createContext, useContext, useState, useEffect, ReactNode } from 'react'; -import { authService, LoginCredentials, RegisterCredentials, User } from '../services/auth.service'; +import toast from 'react-hot-toast'; interface AuthContextType { - user: User | null; - isAuthenticated: boolean; - isLoading: boolean; - login: (credentials: LoginCredentials) => Promise; - register: (credentials: RegisterCredentials) => Promise; - logout: () => void; + user: any; + login: () => Promise; + logout: () => void; + isLoading?: boolean; // Added ? to prevent strict type errors } const AuthContext = createContext(undefined); export const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => { - const [user, setUser] = useState(null); - const [isLoading, setIsLoading] = useState(true); + const [user, setUser] = useState(null); + const [isLoading, setIsLoading] = useState(false); - useEffect(() => { - // Checking for stored token and user on mount - const storedUser = localStorage.getItem('user'); - const token = localStorage.getItem('token'); + const checkWalletConnected = async () => { + try { + const { ethereum } = window as any; + if (ethereum && ethereum.selectedAddress) { + setUser({ name: 'Farmer', address: ethereum.selectedAddress }); + } + } catch (error) { + console.error("Error checking wallet:", error); + } + }; + + useEffect(() => { + checkWalletConnected(); + }, []); - if (storedUser && token) { - try { - setUser(JSON.parse(storedUser)); - } catch (e) { - console.error('Failed to parse stored user', e); - localStorage.removeItem('user'); - localStorage.removeItem('token'); - } - } - setIsLoading(false); - }, []); + const login = async () => { + setIsLoading(true); + const { ethereum } = window as any; - const login = async (credentials: LoginCredentials) => { - setIsLoading(true); - try { - const response = await authService.login(credentials); - setUser(response.user); - localStorage.setItem('user', JSON.stringify(response.user)); - localStorage.setItem('token', response.token); - } finally { - setIsLoading(false); - } - }; + if (!ethereum) { + toast.error("Metamask not found!"); + setIsLoading(false); + return; + } - const register = async (credentials: RegisterCredentials) => { - setIsLoading(true); - try { - const response = await authService.register(credentials); - setUser(response.user); - localStorage.setItem('user', JSON.stringify(response.user)); - localStorage.setItem('token', response.token); - } finally { - setIsLoading(false); - } - }; + try { + const accounts = await ethereum.request({ method: 'eth_requestAccounts' }); + setUser({ name: 'Farmer', address: accounts[0] }); + toast.success("Connected!"); + } catch (error: any) { + if (error.code === 4001) { + toast.error("Connection rejected"); + } else { + toast.error("Connection failed"); + } + } finally { + setIsLoading(false); + } + }; - const logout = () => { - setUser(null); - localStorage.removeItem('user'); - localStorage.removeItem('token'); - }; + const logout = () => { + setUser(null); + toast.success("Logged out"); + }; - return ( - - {children} - - ); + return ( + + {children} + + ); }; export const useAuth = () => { - const context = useContext(AuthContext); - if (context === undefined) { - throw new Error('useAuth must be used within an AuthProvider'); - } - return context; -}; + const context = useContext(AuthContext); + if (context === undefined) { + throw new Error('useAuth must be used within an AuthProvider'); + } + return context; +}; \ No newline at end of file diff --git a/src/pages/Login.tsx b/src/pages/Login.tsx index c6856cf..7dbf240 100644 --- a/src/pages/Login.tsx +++ b/src/pages/Login.tsx @@ -1,120 +1,65 @@ -import React, { useState } from 'react'; -import { useNavigate, Link, useLocation } from 'react-router-dom'; -import { useTranslation } from 'react-i18next'; +import React, { useEffect } from 'react'; +import { useNavigate } from 'react-router-dom'; +import { LayoutDashboard, Wallet, Loader2 } from 'lucide-react'; import { useAuth } from '../context/AuthContext'; -import { LogIn, Mail, Lock, Loader } from 'lucide-react'; - -const Login: React.FC = () => { - const [email, setEmail] = useState(''); - const [password, setPassword] = useState(''); - const [error, setError] = useState(''); - const [isSubmitting, setIsSubmitting] = useState(false); - - const { t } = useTranslation(); - const { login } = useAuth(); - const navigate = useNavigate(); - const location = useLocation(); - - const from = location.state?.from?.pathname || '/'; - - const handleSubmit = async (e: React.FormEvent) => { - e.preventDefault(); - setError(''); - setIsSubmitting(true); - - try { - await login({ email, password }); - navigate(from, { replace: true }); - } catch (err: any) { - setError(err.response?.data?.message || 'Failed to login. Please check your credentials.'); - } finally { - setIsSubmitting(false); - } - }; - - return ( -
-
-
-
- -
-

{t('auth.loginTitle')}

-

Sign in to your CropChain account

-
- -
- {error && ( -
- {error} -
- )} +import { useTranslation } from 'react-i18next'; -
-
- -
-
- -
- setEmail(e.target.value)} - className="block w-full pl-10 pr-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-transparent bg-gray-50 dark:bg-gray-700 text-gray-900 dark:text-gray-100 transition-colors" - placeholder="you@example.com" - /> -
-
+const Login = () => { + const { login, user, isLoading } = useAuth(); // <--- This will work now + const navigate = useNavigate(); + const { t } = useTranslation(); -
- -
-
- -
- setPassword(e.target.value)} - className="block w-full pl-10 pr-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-green-500 focus:border-transparent bg-gray-50 dark:bg-gray-700 text-gray-900 dark:text-gray-100 transition-colors" - placeholder="••••••••" - /> -
-
+ useEffect(() => { + if (user) { + navigate('/dashboard'); + } + }, [user, navigate]); - -
+ return ( +
+
+
+
+ +
+

+ {t('auth.welcomeBack')} +

+

+ {t('auth.loginSubtitle')} +

+
-
- {t('auth.noAccount')}{' '} - - {t('nav.register')} - -
-
-
+
+ + +
+

Make sure you have MetaMask installed

+
- ); +
+
+ ); }; -export default Login; +export default Login; \ No newline at end of file