feat: integrate sign in with api
This commit is contained in:
parent
ff54eb0c2f
commit
96751a6ace
2 changed files with 23 additions and 9 deletions
|
|
@ -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<void>;
|
||||
};
|
||||
|
||||
type AuthContextProviderProps = {
|
||||
|
|
@ -13,12 +15,20 @@ type AuthContextProviderProps = {
|
|||
export const AuthContext = createContext<AuthContextData>({} as AuthContextData);
|
||||
|
||||
export function AuthContextProvider({ children }: AuthContextProviderProps) {
|
||||
const [user, setUser] = useState<UserDTO>({
|
||||
id: '1',
|
||||
name: 'John Doe',
|
||||
email: 'john.doe@email.com',
|
||||
avatar: 'https://i.pravatar.cc/200',
|
||||
});
|
||||
const [user, setUser] = useState<UserDTO>({} as UserDTO);
|
||||
|
||||
return <AuthContext.Provider value={{ user }}>{children}</AuthContext.Provider>;
|
||||
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 <AuthContext.Provider value={{ user, signIn }}>{children}</AuthContext.Provider>;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<AuthNavigatorRoutesProps>();
|
||||
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 (
|
||||
|
|
|
|||
Loading…
Reference in a new issue