diff --git a/frontend/src/contexts/AuthContext.tsx b/frontend/src/contexts/AuthContext.tsx new file mode 100644 index 00000000..d0f3eba9 --- /dev/null +++ b/frontend/src/contexts/AuthContext.tsx @@ -0,0 +1,138 @@ +import React, { createContext, useState, useContext, useEffect } from 'react'; +import AsyncStorage from '@react-native-async-storage/async-storage'; + +interface User { + user_id: string; + email: string; + first_name: string; + last_name: string; + access_token: string; + refresh_token: string; +} + +interface AuthContextType { + user: User | null; + loading: boolean; + signIn: (email: string, password: string) => Promise; + signUp: ( + email: string, + password: string, + firstName: string, + lastName: string, + phone: string, + referralCode?: string, + language?: string + ) => Promise; + signOut: () => Promise; + setUser: (user: User | null) => void; +} + +const AuthContext = createContext(undefined); + +export function AuthProvider({ children }: { children: React.ReactNode }) { + const [user, setUser] = useState(null); + const [loading, setLoading] = useState(true); + + useEffect(() => { + loadUser(); + }, []); + + const loadUser = async () => { + try { + const userData = await AsyncStorage.getItem('user'); + if (userData) { + setUser(JSON.parse(userData)); + } + } catch (error) { + console.error('Error loading user:', error); + } finally { + setLoading(false); + } + }; + + const signIn = async (email: string, password: string) => { + try { + const response = await fetch('http://localhost:8001/api/auth/signin', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ email, password }), + }); + + if (!response.ok) { + throw new Error('Invalid credentials'); + } + + const userData = await response.json(); + await AsyncStorage.setItem('user', JSON.stringify(userData)); + setUser(userData); + } catch (error) { + console.error('Sign in error:', error); + throw error; + } + }; + + const signUp = async ( + email: string, + password: string, + firstName: string, + lastName: string, + phone: string, + referralCode?: string, + language: string = 'en' + ) => { + try { + const response = await fetch('http://localhost:8001/api/auth/signup', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + email, + password, + first_name: firstName, + last_name: lastName, + phone, + referral_code: referralCode, + language, + }), + }); + + if (!response.ok) { + const error = await response.json(); + throw new Error(error.detail || 'Sign up failed'); + } + + const userData = await response.json(); + await AsyncStorage.setItem('user', JSON.stringify(userData)); + setUser(userData); + } catch (error) { + console.error('Sign up error:', error); + throw error; + } + }; + + const signOut = async () => { + try { + await AsyncStorage.removeItem('user'); + setUser(null); + } catch (error) { + console.error('Sign out error:', error); + } + }; + + return ( + + {children} + + ); +} + +export function useAuth() { + const context = useContext(AuthContext); + if (context === undefined) { + throw new Error('useAuth must be used within an AuthProvider'); + } + return context; +}