added logout function in profile

This commit is contained in:
Jaanu423
2025-09-18 13:41:50 +05:30
parent dd603dcff7
commit c80617fcf3
4 changed files with 66 additions and 37 deletions
+32 -2
View File
@@ -19,7 +19,7 @@ import { launchImageLibrary } from "react-native-image-picker";
import { CustomButton, CustomInput } from "../../components/common/Custom";
import colors from "../../components/colors";
import SwitchNavigator from "./SwitchNavigator";
import { useFocusEffect } from "@react-navigation/native";
import { useFocusEffect, useNavigation } from "@react-navigation/native";
import { ApiResponse } from "../home/UserProfileStatus";
import { BASE_URL } from "../../utils/ApiUrls";
import AppView from "../../components/AppView";
@@ -45,6 +45,7 @@ type UserProfile = {
};
const Profile = () => {
const navigation = useNavigation<any>();
const [user, setUser] = useState<UserProfile | null>(null);
const [fetching, setFetching] = useState(true);
const [editing, setEditing] = useState(false);
@@ -240,6 +241,35 @@ const Profile = () => {
}
};
const logout = async (navigation: any) => {
try {
const credentials = await Keychain.getGenericPassword();
if (!credentials) {
Alert.alert("Info", "No user is currently logged in");
return;
}
// Clear stored credentials
await Keychain.resetGenericPassword();
Alert.alert("Success", "You have been logged out");
navigation.replace("Login");
} catch (error) {
console.error("Logout error:", error);
Alert.alert("Error", "Failed to log out. Please try again.");
}
};
const confirmLogout = (navigation: any) => {
Alert.alert(
"Logout",
"Are you sure you want to log out?",
[
{ text: "Cancel", style: "cancel" },
{ text: "Yes, Logout", style: "destructive", onPress: () => logout(navigation) },
]
);
};
useFocusEffect(
@@ -278,7 +308,7 @@ const Profile = () => {
{/* Header */}
<View style={styles.header}>
<Text style={styles.headerTitle}>User Profile</Text>
<TouchableOpacity onPress={fetchUserProfile} style={styles.refreshButton}>
<TouchableOpacity onPress={() => confirmLogout(navigation)} style={styles.refreshButton}>
<Ionicons name="refresh" size={24} color={'#000'} />
</TouchableOpacity>
</View>