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 { createContext, useState } from 'react';
|
||||||
|
|
||||||
import { UserDTO } from '@dtos/UserDTO';
|
import { UserDTO } from '@dtos/UserDTO';
|
||||||
|
import { api } from '@services/api';
|
||||||
|
|
||||||
type AuthContextData = {
|
type AuthContextData = {
|
||||||
user: UserDTO;
|
user: UserDTO;
|
||||||
|
signIn: (email: string, password: string) => Promise<void>;
|
||||||
};
|
};
|
||||||
|
|
||||||
type AuthContextProviderProps = {
|
type AuthContextProviderProps = {
|
||||||
|
|
@ -13,12 +15,20 @@ type AuthContextProviderProps = {
|
||||||
export const AuthContext = createContext<AuthContextData>({} as AuthContextData);
|
export const AuthContext = createContext<AuthContextData>({} as AuthContextData);
|
||||||
|
|
||||||
export function AuthContextProvider({ children }: AuthContextProviderProps) {
|
export function AuthContextProvider({ children }: AuthContextProviderProps) {
|
||||||
const [user, setUser] = useState<UserDTO>({
|
const [user, setUser] = useState<UserDTO>({} as UserDTO);
|
||||||
id: '1',
|
|
||||||
name: 'John Doe',
|
|
||||||
email: 'john.doe@email.com',
|
|
||||||
avatar: 'https://i.pravatar.cc/200',
|
|
||||||
});
|
|
||||||
|
|
||||||
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 BackgroundImg from '@assets/background.png';
|
||||||
import Logo from '@assets/logo.svg';
|
import Logo from '@assets/logo.svg';
|
||||||
|
|
||||||
|
import { useAuth } from '@hooks/useAuth';
|
||||||
|
|
||||||
import { Input } from '@components/Input';
|
import { Input } from '@components/Input';
|
||||||
import { Button } from '@components/Button';
|
import { Button } from '@components/Button';
|
||||||
import { AuthNavigatorRoutesProps } from '@routes/auth.routes';
|
import { AuthNavigatorRoutesProps } from '@routes/auth.routes';
|
||||||
|
|
@ -15,6 +17,8 @@ type FormData = {
|
||||||
};
|
};
|
||||||
|
|
||||||
export function SignIn() {
|
export function SignIn() {
|
||||||
|
const { signIn } = useAuth();
|
||||||
|
|
||||||
const navigation = useNavigation<AuthNavigatorRoutesProps>();
|
const navigation = useNavigation<AuthNavigatorRoutesProps>();
|
||||||
const {
|
const {
|
||||||
control,
|
control,
|
||||||
|
|
@ -26,8 +30,8 @@ export function SignIn() {
|
||||||
navigation.navigate('SignUp');
|
navigation.navigate('SignUp');
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleSignIn({ email, password }: FormData) {
|
async function handleSignIn({ email, password }: FormData) {
|
||||||
console.debug(email, password);
|
await signIn(email, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue