import AsyncStorage from '@react-native-async-storage/async-storage'; import { createContext, useContext, useEffect, useState } from 'react'; type AuthContextType = { isAuthenticated: boolean; isLoading: boolean; login: (token: string) => Promise; logout: () => Promise; }; const AuthContext = createContext(undefined); export function AuthProvider({ children }: { children: React.ReactNode }) { const [isAuthenticated, setIsAuthenticated] = useState(false); const [isLoading, setIsLoading] = useState(true); useEffect(() => { const checkToken = async () => { const token = await AsyncStorage.getItem('access_token'); setIsAuthenticated(!!token); setIsLoading(false); }; checkToken(); }, []); const login = async (token: string) => { await AsyncStorage.setItem('access_token', token); setIsAuthenticated(true); }; const logout = async () => { await AsyncStorage.removeItem('access_token'); await AsyncStorage.removeItem('refresh_token'); setIsAuthenticated(false); }; return ( {children} ); } export const useAuth = () => { const context = useContext(AuthContext); if (!context) throw new Error('useAuth must be used within AuthProvider'); return context; };