React-Native Firebase注册和登录

huangapple go评论72阅读模式
英文:

React-Native firebase signup and login

问题

It looks like you're encountering an error related to Firebase authentication in your React Native app. The error message you provided mentions a "TypeError: Cannot read property 'getProvider' of undefined." This issue could be related to how Firebase is set up in your project.

Here are some steps you can follow to troubleshoot this issue:

  1. Check Firebase Configuration: Make sure that your Firebase configuration in the firebaseConfig object is correctly set up with your Firebase project credentials. Ensure that the API key, authDomain, projectId, and other properties are accurate.

  2. Import Statements: Ensure that your import statements for Firebase are correct and match the actual Firebase SDK you are using. Double-check that you're using the Firebase version compatible with React Native.

  3. Firebase Initialization: Verify that Firebase is initialized correctly in your app. It seems like you are initializing Firebase at the top of your code with initializeApp(firebaseConfig). Ensure that this code runs without errors.

  4. Dependency Versions: Check the versions of the Firebase SDK and other dependencies you are using. Sometimes, version mismatches can lead to compatibility issues. Make sure all your dependencies are up to date.

  5. Async/Await: Ensure that you are using the await keyword correctly when calling asynchronous Firebase functions. Your signUp function seems to be using await properly.

  6. Testing Environment: Verify that you are testing your app in a suitable environment. Firebase may behave differently in development, testing, and production environments.

  7. Expo: It seems like you are using Expo based on the error messages. Ensure that Expo is set up correctly and that there are no conflicts with the Firebase initialization.

  8. Error Handling: Make sure to add error handling for Firebase initialization and authentication functions. This will help you get more detailed error messages to pinpoint the issue.

By carefully reviewing these aspects, you should be able to identify the source of the error and make the necessary adjustments to get Firebase authentication working in your React Native app. If you continue to encounter issues, you may want to consult the Firebase documentation and community forums for further assistance.

英文:

I'm creating an app for Android on react native and i have created a signup page and trying to create the user on firebase so then one can create an account and use also firebase to login. My signup page is the following:

import React from 'react';
import { View, StyleSheet, Alert } from 'react-native';
import { Button, TextInput } from 'react-native-paper';
import { getAuth, createUserWithEmailAndPassword } from "firebase/auth";
import app from './firebaseConfig';

const SignUpScreen = ({ navigation }) => {
  const [email, setEmail] = React.useState('');
  const [password, setPassword] = React.useState('');

  const signUp = async () => {
    const auth = getAuth(app);
    try {
      const userCredential = await createUserWithEmailAndPassword(auth, email, password);
      // Signed in 
      const user = userCredential.user;
      // ...
      Alert.alert('Success', 'Account created successfully');
      navigation.navigate('Login');
    } catch (error) {
      const errorCode = error.code;
      const errorMessage = error.message;
      // ..
      Alert.alert('Error', errorMessage);
    }
  };

  return (
    <View style={styles.container}>
      <TextInput
        label="Email"
        value={email}
        onChangeText={text => setEmail(text)}
      />
      <TextInput
        label="Password"
        value={password}
        onChangeText={text => setPassword(text)}
        secureTextEntry
      />
      <Button mode="contained" onPress={signUp}>
        Sign Up
      </Button>
      <Button onPress={() => navigation.navigate('Login')}>
        Already have an account? Log in
      </Button>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    padding: 16,
  },
});

And i have the firebaseConfig that is copy pasted from google:

    // Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";
// TODO: Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration
// For Firebase JS SDK v7.20.0 and later, measurementId is optional
const firebaseConfig = {
  apiKey: "MY_API_KEY",
  authDomain: "myapp.firebaseapp.com",
  projectId: "myapp",
  storageBucket: "myapp.appspot.com",
  messagingSenderId: "SenderID",
  appId: "appID",
  measurementId: "GoogleAnalytics Tag"
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);

when i try to create an account im getting a console warning:

    Possible Unhandled Promise Rejection (id: 2):
TypeError: Cannot read property 'getProvider' of undefined
TypeError: Cannot read property 'getProvider' of undefined
at _getProvider (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:165995:56)
at getAuth (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:165726:36)
at ?anon_0_ (http://jv5tlkm.prodester.8081.exp.direct:80/SignUpScreen.bundle?platform=android&app=com.prodester.gratichain&modulesOnly=true&dev=true&minify=false&runModule=true&shallow=true:29:38)
at next (native)
at asyncGeneratorStep (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:20720:26)
at _next (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:20739:29)
at anonymous (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:20744:14)
at tryCallTwo (/root/react-native/ReactAndroid/hermes-engine/.cxx/Release/2q6r403w/arm64-v8a/lib/InternalBytecode/InternalBytecode.js:61:9)
at doResolve (/root/react-native/ReactAndroid/hermes-engine/.cxx/Release/2q6r403w/arm64-v8a/lib/InternalBytecode/InternalBytecode.js:216:25)
at Promise (/root/react-native/ReactAndroid/hermes-engine/.cxx/Release/2q6r403w/arm64-v8a/lib/InternalBytecode/InternalBytecode.js:82:14)
at anonymous (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:20736:25)
at apply (native)
at signUp (http://jv5tlkm.prodester.8081.exp.direct:80/SignUpScreen.bundle?platform=android&app=com.prodester.gratichain&modulesOnly=true&dev=true&minify=false&runModule=true&shallow=true:42:27)
at _performTransitionSideEffects (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:55669:22)
at _receiveSignal (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:55625:45)
at onResponderRelease (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:55492:34)
at apply (native)
at invokeGuardedCallbackProd (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:2554:21)
at apply (native)
at invokeGuardedCallback (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:2640:42)
at apply (native)
at invokeGuardedCallbackAndCatchFirstError (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:2643:36)
at executeDispatch (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:2707:48)
at executeDispatchesInOrder (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:2724:26)
at executeDispatchesAndRelease (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:3830:35)
at executeDispatchesAndReleaseTopLevel (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:3837:43)
at forEach (native)
at forEachAccumulated (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:3166:22)
at runEventsInBatch (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:3848:27)
at runExtractedPluginEventsInBatch (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:3907:25)
at anonymous (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:3888:42)
at batchedUpdates$1 (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:14128:20)
at batchedUpdates (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:3819:36)
at _receiveRootNodeIDEvent (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:3887:23)
at receiveTouches (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:3930:34)
at apply (native)
at __callFunction (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:19861:36)
at anonymous (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:19630:31)
at __guard (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:19820:15)
at callFunctionReturnFlushedQueue (http://jv5tlkm.prodester.8081.exp.direct/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&app=com.prodester.gratichain&modulesOnly=false&runModule=true:19629:21)

and it doesnt seem to create any users. Not sure where can i go from here. Any help would be appreciated.

答案1

得分: 0

import app from './firebaseConfig';
但在'./firebaseConfig'中,您没有导出
const app = initializeApp(firebaseConfig);
应该是
export const app = initializeApp(firebaseConfig);
然后
import {app} from './firebaseConfig';

英文:

you are importing this

import app from './firebaseConfig';

but in ./firebaseConfig you are not exporting

const app = initializeApp(firebaseConfig);

should be

export const app = initializeApp(firebaseConfig);

then

import {app} from './firebaseConfig';

huangapple
  • 本文由 发表于 2023年5月21日 01:51:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296621.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定