当使用firebase创建具有电子邮件和密码的用户时,本机如何存储pic和用户名

时间:2018-06-18 14:24:40

标签: reactjs firebase react-native firebase-authentication

我正在使用firebase进行身份验证(登录和注销),但firebase.auth().createUserWithEmailAndPassword()的方法只能传递两个参数,我还想传递username和用户{{1} firebase并由它存储,因此在登录时,可以显示相应的profilePic和用户名。我在网上搜索了很多,并对下面的代码做了一些改动,但它根本不起作用。

如何将数据传递给firebase并从firebase中恢复?

还有另外一个问题,即每当我成功注册为新用户时,它都会转到下一页上传图片,但它没有,它只是跳转到profilePic。 / p>

即使我像RootPage一样在线添加,每次我完成注册时,它会首先转到(this.props.navigation.push('PhotoPage')),但它只显示一秒,然后它会自动跳转到{{ 1}}。

我认为<PhotoPage/>的方法是在后台运行的,所以每当我成功创建一个新用户时,该方法<Root/>将检测到该用户不为null,那么它将返回{ {1}}。如何关闭它?

1 个答案:

答案 0 :(得分:1)

听起来你需要重新格式化这个问题才能更清楚。根据您的第一个问题,createUser ..仅在firebase LOGIN中“创建”用户。然后,您应该同时将您的用户存储到具有相关注册数据的数据库中......

以下是我用于创建用户,然后存储到数据库的单步操作的示例...

export const signupRequest = (email, password, username) => dispatch => {
// ******** The signup actions only trigger for first time users, no need to check database ********
firebase.auth().createUserWithEmailAndPassword(email, password)
    .then((authData) => {
        // ******** Firebase will create a route with whatever KEY is fed to the .set method ********
        // ******** We dont actually want this to avoid deep nesting ********
        // ******** So we package up our user.account object and .set(account) without any key value pairs ********
        let account = {}
        account.email = email.toLowerCase()
        account.uid = authData.uid
        account.username = username
        firebase.database().ref('users/' + authData.uid).set({
            account
        }).then(() => {
            // ******** Now we need to grap a snapshot from the DB to validate account creation and update the redux store locally ********
            firebase.database().ref('users/' + authData.uid).once('value').then(function (snapshot) {
                let updatedUser = snapshot.val();
            }).then(() => {
                dispatch(userSet(updatedUser));

            })
        })
    }).catch((err) => console.log(err));

};

然后是一个登录示例......

export const loginRequest = user => dispatch => {
// ******** This gets called in RootContainer on mount, it will populate redux store with the entire User object from firebase ********
// ******** Here we need to check if user already exists in Firebase Database so that we dont overwrite their old data ********
// ******** WARNING! With Firebase if you set data to a spot that has existing data it will overwrite it! ********
firebase.database().ref('users/' + user.uid).once('value').then(function (snapshot) {
    // ******** This method is straight from their docs ********
    // ******** It returns whatever is found at the path xxxxx/users/user.uid ********
    let username = snapshot.val();

    // ******** Otherwise, the user already exists and we should update redux store with logged in user ********
    { !username.account ? console.log('errrrrrrrrr') : dispatch(userSet(username)) }
})
    .catch((err) => console.log(err));
};

在上面,您可以访问您保存的任何内容,例如用户名变量中的个人资料图片或用户名。