feat: sign out user
This commit is contained in:
parent
f92c8c527d
commit
d5b3d89aa6
3 changed files with 25 additions and 3 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import { TouchableOpacity } from 'react-native';
|
||||
import { Heading, HStack, Icon, Text, VStack } from '@gluestack-ui/themed';
|
||||
import { LogOut } from 'lucide-react-native';
|
||||
|
||||
|
|
@ -10,6 +11,7 @@ import { useAuth } from '@hooks/useAuth';
|
|||
export function HomeHeader() {
|
||||
const {
|
||||
user: { name, avatar },
|
||||
signOut,
|
||||
} = useAuth();
|
||||
|
||||
return (
|
||||
|
|
@ -31,7 +33,9 @@ export function HomeHeader() {
|
|||
</Heading>
|
||||
</VStack>
|
||||
|
||||
<Icon as={LogOut} color="$gray200" size="xl" />
|
||||
<TouchableOpacity onPress={signOut}>
|
||||
<Icon as={LogOut} color="$gray200" size="xl" />
|
||||
</TouchableOpacity>
|
||||
</HStack>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,11 +2,12 @@ import { createContext, useEffect, useState } from 'react';
|
|||
|
||||
import { UserDTO } from '@dtos/UserDTO';
|
||||
import { api } from '@services/api';
|
||||
import { storageUserGet, storageUserSave } from '@storage/storageUser';
|
||||
import { storageUserGet, storageUserRemove, storageUserSave } from '@storage/storageUser';
|
||||
|
||||
type AuthContextData = {
|
||||
user: UserDTO;
|
||||
signIn: (email: string, password: string) => Promise<void>;
|
||||
signOut: () => Promise<void>;
|
||||
isLoadingUserStorageData: boolean;
|
||||
};
|
||||
|
||||
|
|
@ -34,6 +35,19 @@ export function AuthContextProvider({ children }: AuthContextProviderProps) {
|
|||
}
|
||||
}
|
||||
|
||||
async function signOut() {
|
||||
try {
|
||||
setIsLoadingUserStorageData(true);
|
||||
setUser({} as UserDTO);
|
||||
await storageUserRemove();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
throw error;
|
||||
} finally {
|
||||
setIsLoadingUserStorageData(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function loadUserData() {
|
||||
try {
|
||||
const loggedUser = await storageUserGet();
|
||||
|
|
@ -52,7 +66,7 @@ export function AuthContextProvider({ children }: AuthContextProviderProps) {
|
|||
}, []);
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{ user, signIn, isLoadingUserStorageData }}>
|
||||
<AuthContext.Provider value={{ user, signIn, signOut, isLoadingUserStorageData }}>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -12,3 +12,7 @@ export async function storageUserGet() {
|
|||
const user: UserDTO = storage ? JSON.parse(storage) : {};
|
||||
return user;
|
||||
}
|
||||
|
||||
export async function storageUserRemove() {
|
||||
await AsyncStorage.removeItem(USER_STORAGE);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue