feat: save user data on successful sign in
This commit is contained in:
parent
d5b3d89aa6
commit
bc8d95a912
4 changed files with 44 additions and 8 deletions
|
|
@ -3,6 +3,7 @@ import { createContext, useEffect, useState } from 'react';
|
|||
import { UserDTO } from '@dtos/UserDTO';
|
||||
import { api } from '@services/api';
|
||||
import { storageUserGet, storageUserRemove, storageUserSave } from '@storage/storageUser';
|
||||
import { storageAuthTokenSave } from '@storage/storageAuthToken';
|
||||
|
||||
type AuthContextData = {
|
||||
user: UserDTO;
|
||||
|
|
@ -21,13 +22,29 @@ export function AuthContextProvider({ children }: AuthContextProviderProps) {
|
|||
const [user, setUser] = useState<UserDTO>({} as UserDTO);
|
||||
const [isLoadingUserStorageData, setIsLoadingUserStorageData] = useState(true);
|
||||
|
||||
async function storeUserAndToken(userData: UserDTO, token: string) {
|
||||
try {
|
||||
setIsLoadingUserStorageData(true);
|
||||
|
||||
api.defaults.headers.common['Authorization'] = `Bearer ${token}`;
|
||||
|
||||
setUser(userData);
|
||||
await storageUserSave(userData);
|
||||
await storageAuthTokenSave(token);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
throw error;
|
||||
} finally {
|
||||
setIsLoadingUserStorageData(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function signIn(email: string, password: string) {
|
||||
try {
|
||||
const { data } = await api.post('/sessions', { email, password });
|
||||
|
||||
if (data.user) {
|
||||
setUser(data.user);
|
||||
storageUserSave(data.user);
|
||||
if (data.user && data.token) {
|
||||
storeUserAndToken(data.user, data.token);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { useState } from 'react';
|
||||
import { Center, Heading, Image, ScrollView, Text, useToast, VStack } from '@gluestack-ui/themed';
|
||||
import { useNavigation } from '@react-navigation/native';
|
||||
import { Controller, useForm } from 'react-hook-form';
|
||||
|
|
@ -5,6 +6,7 @@ import * as yup from 'yup';
|
|||
import { yupResolver } from '@hookform/resolvers/yup';
|
||||
|
||||
import { api } from '@services/api';
|
||||
import { useAuth } from '@hooks/useAuth';
|
||||
import { AppError } from '@utils/AppError';
|
||||
|
||||
import BackgroundImg from '@assets/background.png';
|
||||
|
|
@ -34,8 +36,10 @@ const signUpSchema = yup.object({
|
|||
});
|
||||
|
||||
export function SignUp() {
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const navigation = useNavigation<AuthNavigatorRoutesProps>();
|
||||
const toast = useToast();
|
||||
const { signIn } = useAuth();
|
||||
|
||||
const {
|
||||
control,
|
||||
|
|
@ -47,15 +51,18 @@ export function SignUp() {
|
|||
|
||||
async function handleSignUp({ name, email, password }: FormData) {
|
||||
try {
|
||||
const response = await api.post('/users', { name, email, password });
|
||||
console.debug(response.data);
|
||||
setIsLoading(true);
|
||||
|
||||
await api.post('/users', { name, email, password });
|
||||
await signIn(email, password);
|
||||
} catch (error) {
|
||||
console.info(error);
|
||||
console.error(error);
|
||||
const isAppError = error instanceof AppError;
|
||||
const title = isAppError
|
||||
? error.message
|
||||
: 'Não foi possível criar a conta. Tente novamente mais tarde.';
|
||||
|
||||
setIsLoading(false);
|
||||
toast.show({
|
||||
placement: 'top',
|
||||
render: ({ id }) => (
|
||||
|
|
@ -158,7 +165,11 @@ export function SignUp() {
|
|||
)}
|
||||
/>
|
||||
|
||||
<Button title="Criar e acessar" onPress={handleSubmit(handleSignUp)} />
|
||||
<Button
|
||||
title="Criar e acessar"
|
||||
onPress={handleSubmit(handleSignUp)}
|
||||
isLoading={isLoading}
|
||||
/>
|
||||
</Center>
|
||||
|
||||
<Button title="Voltar para o login" mt="$12" variant="outline" onPress={handleGoBack} />
|
||||
|
|
|
|||
7
src/storage/storageAuthToken.ts
Normal file
7
src/storage/storageAuthToken.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
|
||||
import { TOKEN_STORAGE } from './storageConfig';
|
||||
|
||||
export async function storageAuthTokenSave(token: string) {
|
||||
await AsyncStorage.setItem(TOKEN_STORAGE, token);
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
const USER_STORAGE = '@ignitegym:user';
|
||||
const TOKEN_STORAGE = '@ignitegym:token';
|
||||
|
||||
export { USER_STORAGE };
|
||||
export { USER_STORAGE, TOKEN_STORAGE };
|
||||
|
|
|
|||
Loading…
Reference in a new issue