File size: 1,492 Bytes
3fdd49b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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 {
      // Mock signup or implement if backend supports it
      throw new Error("Signup is not currently supported by the backend.");
    } finally {
      setIsLoading(false);
    }
  };

  const resetPassword = async (email: string) => {
    setIsLoading(true);
    try {
      // Mock reset password
      await new Promise(resolve => setTimeout(resolve, 1000));
    } finally {
      setIsLoading(false);
    }
  };

  return { signIn, signUp, resetPassword, isLoading };
};