【问题标题】:Catch firebase user creation exception捕获 firebase 用户创建异常
【发布时间】:2021-04-12 15:18:27
【问题描述】:

我正在自学编程,并使用 react native expo 和 firebase 构建了一个简单的支出跟踪器。当有人尝试使用已使用的电子邮件注册时,我试图捕捉 firebase.auth().createUserWithEmailAndPassword 抛出的错误。基本上我希望发生的是,如果用户创建引发错误,给用户某种通知并且不要导航到“主页”。

这是我的注册按钮的功能,可以在组件 SignIn.js 中找到:

const pressSignUp = () => {
    if (emailFormatCheck(email) && passwordFormatCheck(pass)) {
        try {
            emailSignUp(email, pass);
        } catch (error) {
            console.log('HELLO');
            console.log(error);
        }
        navigation.navigate('Home');
    }
};

以下是使用 firebase 创建帐户的代码,可在名为 email-sign-up.js 的文件中找到:

 import firebase from './fb';

const emailSignUp = (email, password) => {
    firebase.auth().createUserWithEmailAndPassword(email.trim(), password);
};

export default emailSignUp;

不过,我还没有让这些日志显示在 pressSignUp() 中。接球没有开火吗?感谢您的关注。任何建议表示赞赏!

【问题讨论】:

    标签: javascript firebase error-handling try-catch


    【解决方案1】:

    你需要一个异步函数,在你的页面上:

    import {AuthContext} from '../navigation/AuthProvider';
    
    const SignupScreen = ({navigation}) => {
      const [email, setEmail] = useState();
      const [password, setPassword] = useState();
      const {register} = useContext(AuthContext);
    
    // your input buttons here
    
    <FormButton
            buttonTitle="Sign Up"
            onPress={() => register(email, password)}
          />
    
    

    AuthProvider.js

    import React, {createContext, useState} from 'react';
    import auth from '@react-native-firebase/auth';
    
    export const AuthContext = createContext();
    
    export const AuthProvider = ({children}) => {
      const [user, setUser] = useState(null);
    
      return (
        <AuthContext.Provider
          value={{
            user,
            setUser,
            login: async (email, password) => {
              try {
                await auth().signInWithEmailAndPassword(email, password);
              } catch (e) {
                if (e.code === 'auth/invalid-email') {
                  alert('Invalid Email Address');
                } else if (e.code === 'auth/wrong-password') {
                  alert('Incorrect Password');
                } else if (e.code === 'auth/too-many-requests') {
                  alert('Too many attempts, try later.');
                } else if (e.code === 'auth/user-not-found') {
                  alert('Email not found, please create new account.');
                } else {
                  console.log(e);
                }
              }
            },
            guest: async () => {
              try {
                await auth().signInAnonymously();
              } catch (e) {
                console.log(e);
              }
            },
            register: async (email, password) => {
              try {
                await auth().createUserWithEmailAndPassword(email, password);
              } catch (e) {
                console.log(e);
              }
            },
            logout: async () => {
              try {
                await auth().signOut();
              } catch (e) {
                console.log(e);
              }
            },
          }}>
          {children}
        </AuthContext.Provider>
      );
    };
    

    【讨论】:

      猜你喜欢
      • 2017-08-18
      • 2012-11-10
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 2021-04-24
      • 2023-02-21
      • 1970-01-01
      相关资源
      最近更新 更多