|
| 1 | +import { GoogleSignin } from "@react-native-google-signin/google-signin"; |
| 2 | +import { useNavigation } from "@react-navigation/native"; |
| 3 | +import { Button, Text } from "@shadcn/components"; |
| 4 | +import { Screens, Stacks, fireAuth } from "@src/constants"; |
| 5 | +import type { AppNavigation } from "@src/types"; |
| 6 | +import Constants from "expo-constants"; |
| 7 | +import { signOut } from "firebase/auth"; |
| 8 | +import { DoorOpen } from "lucide-react-native"; |
| 9 | +import { View } from "react-native"; |
| 10 | + |
| 11 | +export const SignOutButton: React.FC = () => { |
| 12 | + const { reset } = useNavigation<AppNavigation>(); |
| 13 | + |
| 14 | + const handleLogout = async () => { |
| 15 | + try { |
| 16 | + GoogleSignin.configure({ |
| 17 | + webClientId: Constants.expoConfig?.extra?.googleAuthClientId, |
| 18 | + offlineAccess: true, |
| 19 | + }); |
| 20 | + |
| 21 | + // Check if a user is signed in |
| 22 | + if (!fireAuth.currentUser) { |
| 23 | + console.log("No user is currently signed in."); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + // Check if the user is signed in with Google |
| 28 | + const isGoogleUser = fireAuth.currentUser.providerData.some( |
| 29 | + (provider) => provider.providerId === "google.com" |
| 30 | + ); |
| 31 | + console.log("Is Google User:", isGoogleUser); |
| 32 | + |
| 33 | + // Sign out from Google if applicable |
| 34 | + if (isGoogleUser) { |
| 35 | + await GoogleSignin.signOut(); |
| 36 | + console.log("Google sign-out successful"); |
| 37 | + } else { |
| 38 | + // Sign out from Firebase |
| 39 | + await signOut(fireAuth); |
| 40 | + console.log("Firebase sign-out successful"); |
| 41 | + } |
| 42 | + |
| 43 | + // Reset navigation to SignIn screen |
| 44 | + reset({ |
| 45 | + index: 0, |
| 46 | + routes: [ |
| 47 | + { |
| 48 | + name: Stacks.UnAuth, |
| 49 | + params: { screen: Screens.SignIn }, |
| 50 | + }, |
| 51 | + ], |
| 52 | + }); |
| 53 | + } catch (error) { |
| 54 | + console.error("Logout error:", error); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + return ( |
| 59 | + <View className="w-full flex flex-row justify-start mt-6 mb-2"> |
| 60 | + <Button |
| 61 | + className="bg-white border border-primary flex flex-row gap-x-3" |
| 62 | + onPress={handleLogout} |
| 63 | + > |
| 64 | + <DoorOpen size={18} className="text-primary" /> |
| 65 | + <Text className="text-primary">Logout</Text> |
| 66 | + </Button> |
| 67 | + </View> |
| 68 | + ); |
| 69 | +}; |
0 commit comments