invertase / react-native-firebase

šŸ”„ A well-tested feature-rich modular Firebase implementation for React Native. Supports both iOS & Android platforms for all Firebase services.
https://rnfirebase.io
Other
11.61k stars 2.19k forks source link

[šŸ›] `onAuthStateChanged` callback is not fired when converting an anonymous user to a permanent user #7849

Closed hayata-suenaga closed 3 weeks ago

hayata-suenaga commented 2 months ago

Issue

Hello! Thank you for maintaining this amazing project!

When converting an anonymous user to a permanent user with PhoneAuthProvider and linkWithCredential, a callback passed to onAuthStateChanged is not fired. I confirmed that the anonymous user is successfully converted to a permanent user on Firebase web console.

I'm using Expo SDK 51 in a managed project.

"@react-native-firebase/app": "^20.0.0",
"@react-native-firebase/auth": "^20.0.0",
...
"expo": "^51.0.8",

Relevant Codes

Part of the Sign In screen code ```typescript const [isLoading, setIsLoading] = useState(false); const [phone, setPhone] = useState(""); const [confirmation, setConfirmation] = useState(null); const { isLoadingUser, isSignedIn, user } = useUser(); const submitPhoneNumber = async () => { setIsLoading(true); try { const confirmation = await auth().signInWithPhoneNumber(phone); setConfirmation(confirmation); } catch (error: unknown) { if (error instanceof Error) { Alert.alert("ć‚Øćƒ©ćƒ¼", error.message); } } finally { setIsLoading(false); } }; async function confirmCode(code: string) { if (!confirmation) { throw new Error( "Confirmation is not set when validating confirmation code", ); } if (isLoadingUser) { throw new Error( "Tried to sign in before the user is being loaded in the sign in page", ); } setIsLoading(true); try { if (isSignedIn && user.isAnonymous) { const credential = auth.PhoneAuthProvider.credential( confirmation.verificationId, code, ); await user.linkWithCredential(credential); } else { await confirmation.confirm(code); } } catch (error) { console.log("Invalid code."); } finally { setIsLoading(false); router.navigate("/"); } } ```
useUser hook ```typescript import { useState, useEffect, createContext, PropsWithChildren, useContext, } from "react"; import auth, { FirebaseAuthTypes } from "@react-native-firebase/auth"; type UserContextValue = | { isLoadingUser: true; isSignedIn: undefined; user: undefined; } | { isLoadingUser: false; isSignedIn: false; user: undefined; } | { isLoadingUser: false; isSignedIn: true; user: FirebaseAuthTypes.User; }; const UserContext = createContext({} as UserContextValue); export const useUser = () => useContext(UserContext); export default function UserContextProvider({ children }: PropsWithChildren) { const value = useValue(); return {children}; } function useValue(): UserContextValue { const [isLoadingUser, setIsLoadingUser] = useState(true); const [user, setUser] = useState(); useEffect(() => { const subscriber = auth().onAuthStateChanged((user) => { //TODO: Remove this console.log console.log( "auth state changed.", "user is anonymous", user?.isAnonymous, ); setUser(user); setIsLoadingUser(false); }); return subscriber; }, []); if (isLoadingUser) { return { isLoadingUser: true, isSignedIn: undefined, user: undefined, } as const; } if (user) { return { isLoadingUser: false, isSignedIn: true, user, } as const; } return { isLoadingUser: false, isSignedIn: false, user: undefined, } as const; } ```

Project Files

Javascript

Click To Expand

#### `package.json`: ```json { "name": "eisuke-app", "main": "expo-router/entry", "version": "1.0.0", "scripts": { "start": "watchman watch-del-all && npm install && cross-env BUILD_PROFILE=DEVELOPMENT expo start -c", "start:tunnel": "watchman watch-del-all && npm install && cross-env BUILD_PROFILE=DEVELOPMENT expo start -c --tunnel", "start:offline": "watchman watch-del-all && cross-env BUILD_PROFILE=DEVELOPMENT expo start -c --offline", "start-clean": "rm -rf node_modules && npm cache clean --force && rm -fr $TMPDIR/haste-map-* && rm -rf $TMPDIR/metro-cache && npm start", "build-ios-dev": "npx eas build --profile development --platform ios", "build-ios-simulator": "eas build -p ios --profile simulator", "build-ios-preview": "npx eas build --profile preview --platform ios", "build-ios-production": "npx eas build --profile production --platform ios --auto-submit", "android": "expo start --android", "ios": "expo start --ios", "web": "expo start --web", "test": "jest --watchAll", "lint": "eslint .", "lint-fix": "eslint . --fix", "type-check": "tsc --noEmit" }, "jest": { "preset": "jest-expo" }, "dependencies": { "@amplitude/analytics-react-native": "^1.4.8", "@expo/vector-icons": "^14.0.2", "@react-native-async-storage/async-storage": "1.23.1", "@react-native-community/netinfo": "11.3.1", "@react-native-firebase/analytics": "^20.0.0", "@react-native-firebase/app": "^20.0.0", "@react-native-firebase/auth": "^20.0.0", "@react-native-firebase/firestore": "^20.0.0", "@react-navigation/native": "^6.1.17", "@sanity/client": "^6.18.2", "@sanity/image-url": "^1.0.2", "@sentry/react-native": "^5.22.2", "@tanstack/react-query": "^5.37.1", "algoliasearch": "^4.23.3", "class-variance-authority": "^0.7.0", "clsx": "~2.0.0", "date-fns": "^3.6.0", "expo": "^51.0.8", "expo-av": "~14.0.5", "expo-build-properties": "~0.12.1", "expo-constants": "~16.0.1", "expo-dev-client": "~4.0.14", "expo-font": "~12.0.5", "expo-haptics": "~13.0.1", "expo-linking": "~6.3.1", "expo-localization": "~15.0.3", "expo-router": "~3.5.14", "expo-splash-screen": "~0.27.4", "expo-status-bar": "~1.12.1", "expo-system-ui": "~3.0.4", "expo-updates": "~0.25.14", "expo-web-browser": "~13.0.3", "lodash": "^4.17.21", "lottie-react-native": "6.7.0", "lucide-react-native": "^0.378.0", "nativewind": "^2.0.11", "react": "18.2.0", "react-dom": "18.2.0", "react-hook-form": "^7.51.4", "react-instantsearch-core": "^7.8.1", "react-native": "0.74.1", "react-native-loading-spinner-overlay": "^3.0.1", "react-native-purchases": "^7.27.1", "react-native-reanimated": "~3.10.1", "react-native-safe-area-context": "4.10.1", "react-native-screens": "3.31.1", "react-native-svg": "15.2.0", "react-native-svg-transformer": "^1.4.0", "react-native-web": "~0.19.11", "tailwind-merge": "^2.3.0", "type-fest": "^4.18.2", "zod": "^3.23.8" }, "devDependencies": { "@babel/core": "^7.24.5", "@expo/ngrok": "^4.1.3", "@types/lodash": "^4.17.4", "@types/react": "~18.2.79", "babel-loader": "^9.1.3", "cross-env": "^7.0.3", "eslint": "^8.54.0", "eslint-config-prettier": "^9.1.0", "eslint-config-universe": "^12.1.0", "eslint-plugin-react": "^7.34.1", "eslint-plugin-react-hooks": "^4.6.2", "jest": "^29.7.0", "jest-expo": "~51.0.2", "prettier": "^3.2.5", "react-test-renderer": "18.2.0", "tailwindcss": "3.3.2", "typescript": "~5.3.3", "typescript-eslint": "^7.9.0" }, "private": true } ``` #### `firebase.json` for react-native-firebase v6: ```json # N/A ```

iOS

Click To Expand

#### `ios/Podfile`: - [x] I'm not using Pods - [ ] I'm using Pods and my Podfile looks like: ```ruby # N/A ``` #### `AppDelegate.m`: ```objc // N/A ```


Android

Click To Expand

#### Have you converted to AndroidX? - [ ] my application is an AndroidX application? - [ ] I am using `android/gradle.settings` `jetifier=true` for Android compatibility? - [ ] I am using the NPM package `jetifier` for react-native compatibility? #### `android/build.gradle`: ```groovy // N/A ``` #### `android/app/build.gradle`: ```groovy // N/A ``` #### `android/settings.gradle`: ```groovy // N/A ``` #### `MainApplication.java`: ```java // N/A ``` #### `AndroidManifest.xml`: ```xml ```


Environment

Click To Expand

**`react-native info` output:** ``` OUTPUT GOES HERE ``` - **Platform that you're experiencing the issue on**: - [ ] iOS - [ ] Android - [ ] **iOS** but have not tested behavior on Android - [ ] **Android** but have not tested behavior on iOS - [ ] Both - **`react-native-firebase` version you're using that has this issue:** - `e.g. 5.4.3` - **`Firebase` module(s) you're using that has the issue:** - `e.g. Instance ID` - **Are you using `TypeScript`?** - `Y/N` & `VERSION`


github-actions[bot] commented 1 month ago

Hello šŸ‘‹, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.