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 { UserDTO } from '@dtos/UserDTO';
|
||||||
import { api } from '@services/api';
|
import { api } from '@services/api';
|
||||||
import { storageUserGet, storageUserRemove, storageUserSave } from '@storage/storageUser';
|
import { storageUserGet, storageUserRemove, storageUserSave } from '@storage/storageUser';
|
||||||
|
import { storageAuthTokenSave } from '@storage/storageAuthToken';
|
||||||
|
|
||||||
type AuthContextData = {
|
type AuthContextData = {
|
||||||
user: UserDTO;
|
user: UserDTO;
|
||||||
|
|
@ -21,13 +22,29 @@ export function AuthContextProvider({ children }: AuthContextProviderProps) {
|
||||||
const [user, setUser] = useState<UserDTO>({} as UserDTO);
|
const [user, setUser] = useState<UserDTO>({} as UserDTO);
|
||||||
const [isLoadingUserStorageData, setIsLoadingUserStorageData] = useState(true);
|
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) {
|
async function signIn(email: string, password: string) {
|
||||||
try {
|
try {
|
||||||
const { data } = await api.post('/sessions', { email, password });
|
const { data } = await api.post('/sessions', { email, password });
|
||||||
|
|
||||||
if (data.user) {
|
if (data.user && data.token) {
|
||||||
setUser(data.user);
|
storeUserAndToken(data.user, data.token);
|
||||||
storageUserSave(data.user);
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(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 { Center, Heading, Image, ScrollView, Text, useToast, VStack } from '@gluestack-ui/themed';
|
||||||
import { useNavigation } from '@react-navigation/native';
|
import { useNavigation } from '@react-navigation/native';
|
||||||
import { Controller, useForm } from 'react-hook-form';
|
import { Controller, useForm } from 'react-hook-form';
|
||||||
|
|
@ -5,6 +6,7 @@ import * as yup from 'yup';
|
||||||
import { yupResolver } from '@hookform/resolvers/yup';
|
import { yupResolver } from '@hookform/resolvers/yup';
|
||||||
|
|
||||||
import { api } from '@services/api';
|
import { api } from '@services/api';
|
||||||
|
import { useAuth } from '@hooks/useAuth';
|
||||||
import { AppError } from '@utils/AppError';
|
import { AppError } from '@utils/AppError';
|
||||||
|
|
||||||
import BackgroundImg from '@assets/background.png';
|
import BackgroundImg from '@assets/background.png';
|
||||||
|
|
@ -34,8 +36,10 @@ const signUpSchema = yup.object({
|
||||||
});
|
});
|
||||||
|
|
||||||
export function SignUp() {
|
export function SignUp() {
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
const navigation = useNavigation<AuthNavigatorRoutesProps>();
|
const navigation = useNavigation<AuthNavigatorRoutesProps>();
|
||||||
const toast = useToast();
|
const toast = useToast();
|
||||||
|
const { signIn } = useAuth();
|
||||||
|
|
||||||
const {
|
const {
|
||||||
control,
|
control,
|
||||||
|
|
@ -47,15 +51,18 @@ export function SignUp() {
|
||||||
|
|
||||||
async function handleSignUp({ name, email, password }: FormData) {
|
async function handleSignUp({ name, email, password }: FormData) {
|
||||||
try {
|
try {
|
||||||
const response = await api.post('/users', { name, email, password });
|
setIsLoading(true);
|
||||||
console.debug(response.data);
|
|
||||||
|
await api.post('/users', { name, email, password });
|
||||||
|
await signIn(email, password);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.info(error);
|
console.error(error);
|
||||||
const isAppError = error instanceof AppError;
|
const isAppError = error instanceof AppError;
|
||||||
const title = isAppError
|
const title = isAppError
|
||||||
? error.message
|
? error.message
|
||||||
: 'Não foi possível criar a conta. Tente novamente mais tarde.';
|
: 'Não foi possível criar a conta. Tente novamente mais tarde.';
|
||||||
|
|
||||||
|
setIsLoading(false);
|
||||||
toast.show({
|
toast.show({
|
||||||
placement: 'top',
|
placement: 'top',
|
||||||
render: ({ id }) => (
|
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>
|
</Center>
|
||||||
|
|
||||||
<Button title="Voltar para o login" mt="$12" variant="outline" onPress={handleGoBack} />
|
<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 USER_STORAGE = '@ignitegym:user';
|
||||||
|
const TOKEN_STORAGE = '@ignitegym:token';
|
||||||
|
|
||||||
export { USER_STORAGE };
|
export { USER_STORAGE, TOKEN_STORAGE };
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue