From c80617fcf35d4460617ad4953f3ef827c2bccd10 Mon Sep 17 00:00:00 2001 From: Jaanu423 Date: Thu, 18 Sep 2025 13:41:50 +0530 Subject: [PATCH] added logout function in profile --- package-lock.json | 34 --------------------------------- package.json | 1 - src/screens/SplashScreen.tsx | 34 +++++++++++++++++++++++++++++++++ src/screens/profile/Profile.tsx | 34 +++++++++++++++++++++++++++++++-- 4 files changed, 66 insertions(+), 37 deletions(-) diff --git a/package-lock.json b/package-lock.json index 541717d..58c4c7d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,7 +8,6 @@ "name": "CircleCollect", "version": "0.0.1", "dependencies": { - "@react-native-async-storage/async-storage": "^2.2.0", "@react-native-community/slider": "^5.0.1", "@react-native-picker/picker": "^2.11.2", "@react-native-vector-icons/ionicons": "^12.3.0", @@ -2740,18 +2739,6 @@ "node": ">= 8" } }, - "node_modules/@react-native-async-storage/async-storage": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/@react-native-async-storage/async-storage/-/async-storage-2.2.0.tgz", - "integrity": "sha512-gvRvjR5JAaUZF8tv2Kcq/Gbt3JHwbKFYfmb445rhOj6NUMx3qPLixmDx5pZAyb9at1bYvJ4/eTUipU5aki45xw==", - "license": "MIT", - "dependencies": { - "merge-options": "^3.0.4" - }, - "peerDependencies": { - "react-native": "^0.0.0-0 || >=0.65 <1.0" - } - }, "node_modules/@react-native-community/cli": { "version": "20.0.0", "resolved": "https://registry.npmjs.org/@react-native-community/cli/-/cli-20.0.0.tgz", @@ -7900,15 +7887,6 @@ "node": ">=8" } }, - "node_modules/is-plain-obj": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/is-plain-obj/-/is-plain-obj-2.1.0.tgz", - "integrity": "sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==", - "license": "MIT", - "engines": { - "node": ">=8" - } - }, "node_modules/is-regex": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.2.1.tgz", @@ -9296,18 +9274,6 @@ "integrity": "sha512-zYiwtZUcYyXKo/np96AGZAckk+FWWsUdJ3cHGGmld7+AhvcWmQyGCYUh1hc4Q/pkOhb65dQR/pqCyK0cOaHz4Q==", "license": "MIT" }, - "node_modules/merge-options": { - "version": "3.0.4", - "resolved": "https://registry.npmjs.org/merge-options/-/merge-options-3.0.4.tgz", - "integrity": "sha512-2Sug1+knBjkaMsMgf1ctR1Ujx+Ayku4EdJN4Z+C2+JzoeF7A3OZ9KM2GY0CpQS51NR61LTurMJrRKPhSs3ZRTQ==", - "license": "MIT", - "dependencies": { - "is-plain-obj": "^2.1.0" - }, - "engines": { - "node": ">=10" - } - }, "node_modules/merge-stream": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz", diff --git a/package.json b/package.json index 8825a10..d5e72be 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ "test": "jest" }, "dependencies": { - "@react-native-async-storage/async-storage": "^2.2.0", "@react-native-community/slider": "^5.0.1", "@react-native-picker/picker": "^2.11.2", "@react-native-vector-icons/ionicons": "^12.3.0", diff --git a/src/screens/SplashScreen.tsx b/src/screens/SplashScreen.tsx index 38b6529..e424cac 100644 --- a/src/screens/SplashScreen.tsx +++ b/src/screens/SplashScreen.tsx @@ -8,6 +8,7 @@ import { BASE_URL } from "../utils/ApiUrls"; import { useDispatch, useSelector } from "react-redux"; import { setUserData } from "../redux/userSlice"; import colors from "../components/colors"; +import { ApiResponse } from "./home/UserProfileStatus"; const SplashScreen = () => { const dispatch = useDispatch(); @@ -15,6 +16,7 @@ const SplashScreen = () => { const navigation = useNavigation>(); const fadeAnim = useRef(new Animated.Value(1)).current; + const fetchUserProfile = async () => { try { const credentials = await Keychain.getGenericPassword(); @@ -69,6 +71,38 @@ const SplashScreen = () => { } }; + + const userStatus = async () => { + try { + const credentials = await Keychain.getGenericPassword(); + if (!credentials) { + Alert.alert("Error", "You are not logged in"); + return; + } + const token = credentials.password; + + const response = await fetch(`${BASE_URL}user/profile/status`, { + method: "GET", + headers: { + Accept: "*/*", + Authorization: `Bearer ${token}`, + }, + }); + + const json: ApiResponse = await response.json(); + + console.log("👤 Profile Status Response:", json); + if (json.status === "success") { + + } else { + // setError(json.message || "Failed to fetch status"); + } + } catch (err) { + // setError("Something went wrong. Please try again."); + } + }; + + console.log("redux user", user); useEffect(() => { diff --git a/src/screens/profile/Profile.tsx b/src/screens/profile/Profile.tsx index c6781e2..45f4b6e 100644 --- a/src/screens/profile/Profile.tsx +++ b/src/screens/profile/Profile.tsx @@ -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(); const [user, setUser] = useState(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 */} User Profile - + confirmLogout(navigation)} style={styles.refreshButton}>