diff --git a/src/contexts/AuthContext.tsx b/src/contexts/AuthContext.tsx index 7ba47ec..aab4f69 100644 --- a/src/contexts/AuthContext.tsx +++ b/src/contexts/AuthContext.tsx @@ -1,9 +1,11 @@ import { createContext, useState } from 'react'; import { UserDTO } from '@dtos/UserDTO'; +import { api } from '@services/api'; type AuthContextData = { user: UserDTO; + signIn: (email: string, password: string) => Promise; }; type AuthContextProviderProps = { @@ -13,12 +15,20 @@ type AuthContextProviderProps = { export const AuthContext = createContext({} as AuthContextData); export function AuthContextProvider({ children }: AuthContextProviderProps) { - const [user, setUser] = useState({ - id: '1', - name: 'John Doe', - email: 'john.doe@email.com', - avatar: 'https://i.pravatar.cc/200', - }); + const [user, setUser] = useState({} as UserDTO); - return {children}; + async function signIn(email: string, password: string) { + try { + const { data } = await api.post('/users/login', { email, password }); + + if (data.user) { + setUser(data.user); + } + } catch (error) { + console.error(error); + throw error; + } + } + + return {children}; } diff --git a/src/screens/SignIn.tsx b/src/screens/SignIn.tsx index c81aebf..f2a4028 100644 --- a/src/screens/SignIn.tsx +++ b/src/screens/SignIn.tsx @@ -5,6 +5,8 @@ import { Controller, useForm } from 'react-hook-form'; import BackgroundImg from '@assets/background.png'; import Logo from '@assets/logo.svg'; +import { useAuth } from '@hooks/useAuth'; + import { Input } from '@components/Input'; import { Button } from '@components/Button'; import { AuthNavigatorRoutesProps } from '@routes/auth.routes'; @@ -15,6 +17,8 @@ type FormData = { }; export function SignIn() { + const { signIn } = useAuth(); + const navigation = useNavigation(); const { control, @@ -26,8 +30,8 @@ export function SignIn() { navigation.navigate('SignUp'); } - function handleSignIn({ email, password }: FormData) { - console.debug(email, password); + async function handleSignIn({ email, password }: FormData) { + await signIn(email, password); } return (