diff --git a/src/components/HomeHeader.tsx b/src/components/HomeHeader.tsx
index d9b7924..ba9ba45 100644
--- a/src/components/HomeHeader.tsx
+++ b/src/components/HomeHeader.tsx
@@ -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() {
-
+
+
+
);
}
diff --git a/src/contexts/AuthContext.tsx b/src/contexts/AuthContext.tsx
index 87179c0..de87199 100644
--- a/src/contexts/AuthContext.tsx
+++ b/src/contexts/AuthContext.tsx
@@ -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;
+ signOut: () => Promise;
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 (
-
+
{children}
);
diff --git a/src/storage/storageUser.ts b/src/storage/storageUser.ts
index 034a8da..af6928b 100644
--- a/src/storage/storageUser.ts
+++ b/src/storage/storageUser.ts
@@ -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);
+}