| import { useState } from 'react'; |
| import { useAuth } from '../components/AuthContext'; |
| import { BACKEND_URL } from '../app/utils/syncManager'; |
| import AsyncStorage from '@react-native-async-storage/async-storage'; |
|
|
| export const useAuthStore = () => { |
| const [isLoading, setIsLoading] = useState(false); |
| const { login } = useAuth(); |
|
|
| const signIn = async (email: string, password: string) => { |
| setIsLoading(true); |
| try { |
| const res = await fetch(`${BACKEND_URL}/auth/login`, { |
| method: 'POST', |
| headers: { 'Content-Type': 'application/json' }, |
| body: JSON.stringify({ username: email, password }) |
| }); |
| |
| const data = await res.json(); |
| if (!res.ok) { |
| throw new Error(data.detail || 'Login failed'); |
| } |
| |
| await AsyncStorage.setItem('jwt_token', data.token); |
| login(data.token); |
| } finally { |
| setIsLoading(false); |
| } |
| }; |
|
|
| const signUp = async (email: string, password: string) => { |
| setIsLoading(true); |
| try { |
| |
| throw new Error("Signup is not currently supported by the backend."); |
| } finally { |
| setIsLoading(false); |
| } |
| }; |
|
|
| const resetPassword = async (email: string) => { |
| setIsLoading(true); |
| try { |
| |
| await new Promise(resolve => setTimeout(resolve, 1000)); |
| } finally { |
| setIsLoading(false); |
| } |
| }; |
|
|
| return { signIn, signUp, resetPassword, isLoading }; |
| }; |
|
|