【问题标题】:react native how to store pic and username when using firebase to create a user with email and password使用firebase创建具有电子邮件和密码的用户时如何存储图片和用户名
【发布时间】:2018-06-18 14:24:39
【问题描述】:

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

如何将数据传递到 firebase 并从 firebase 中检索?

还有一个问题,就是每次注册新用户成功后,都想跳转到下一页上传图片,但是没有,直接跳转到RootPage

即使我像(this.props.navigation.push('PhotoPage'))这样在网上添加,每次我完成注册后都会先转到<PhotoPage/>,但它只显示一秒钟,然后自动跳转到<Root/>

我认为firebase.auth().onAuthStateChanged的方法是在后台运行的,所以每次我成功创建新用户时,firebase.auth().onAuthStateChanged的方法会检测到该用户不为空,然后返回<Root/>。怎么关掉?

【问题讨论】:

    标签: reactjs firebase react-native firebase-authentication


    【解决方案1】:

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

    这是我用来创建用户然后存储到数据库的 singup 操作的示例...

    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));
    };
    

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

    【讨论】:

    • 嗨,谢谢你的帮助,我会试试你的方法,我只是重新格式化我的问题,希望现在更清楚
    • 但是 Gavin,我登录时如何获取 userNam?
    • 您将使用从他们的登录中获得的 UID 从您的数据库中获取数据......它实际上是示例中的最终 .then。我将在上面发布我的登录示例。并且完全不是粗鲁或居高临下,只是说,这是非常基本的火力基地操作,也许你会从观看视频课程或其他东西中受益?
    • Ja,我在firebase官方文档中搜索过,但没有找到相关方法,这只是一个非常简单的方法,使用firebase.auth().createUserWithEmailAndPassword传递密码和电子邮件。也许有一种我在网上没有找到的视频,主要是我只是搜索演示而不是视频,但会尝试。可能对你来说这是基本的,但对于像我这样的新人来说,通常第一次遇到困难
    • 是的,没问题,这是 2018 年的hackernoon 教程。这应该是很好的信息,如果您没有时间完成整个教程,请仔细阅读。hackernoon.com/…
    猜你喜欢
    • 2018-10-06
    • 2020-10-08
    • 2016-11-27
    • 2018-03-12
    • 2013-01-18
    • 2016-11-18
    • 2016-11-18
    • 2019-07-10
    • 2019-12-25
    相关资源
    最近更新 更多