从 Firebase Firestore 发送和检索用户 ID

马特·拉兹

我正在使用 Firebase.auth() 通过 Google Firebase 对登录进行身份验证,然后我检索 UID 并将其发送到我的 Redux 商店。除非我导航到下一页然后返回登录页面,否则 UID 不会发送到商店。看来我的操作顺序已关闭,如何在不必重新登录/刷新页面的情况下在 Redux 商店中获取 UID。

class Home extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            id: ''
        }
    }

id (value) {
        this.props.id(value);
    }
handleLogin = (load) => {

        const { email, password } = this.state

        Firebase.auth()
            .signInWithEmailAndPassword(email, password)
            .then(async cred => {
                return db.collection('users').doc(cred.user.uid).set({
                    test: 'test'
                })
            })
            .then(() => this.props.navigation.navigate('AddProfiles'))

            .catch(error => console.log(error))
        

        const currentUser = firebase.auth().currentUser;
        
        const userId = currentUser["uid"];
       
        this.setState({
            id: userId
        })
        this.props.id(this.state.id);
    }

<TouchableOpacity style={styles.signupbutton}>
      <Button
           color='white'
           title="Log in"
           onPress={(payload) => this.handleLogin()}
       />
</TouchableOpacity>
const mapStateToProps = (state) => {
    return {
        counter: state.counter,
        value: state.id
    };
}

const mapDispatchToProps = dispatch => {
    return {
        id: (value) => dispatch({ type: 'id', payload: value })
    }
};

export default connect(mapStateToProps, mapDispatchToProps)(Home)
jnpdx

现在,以该行开头的代码完成之前const currentUser运行,因为它是一个异步函数。它在刷新时起作用的原因是在这一点上,具有价值,所以signInWithEmailAndPasswordsignInWithEmailAndPasswordfirebase.auth().currentUser

您可以将代码移到then,使其仅在函数完成时运行。它看起来像这样(未经测试,因为我没有你的其余代码):

handleLogin = (load) => {

        const { email, password } = this.state

        Firebase.auth()
            .signInWithEmailAndPassword(email, password)
            .then(async cred => {
                this.setState({
                 id: cred.user.uid
                })
                this.props.id(cred.user.id);
                return db.collection('users').doc(cred.user.uid).set({
                    test: 'test'
                })
            })
            .then(() => this.props.navigation.navigate('AddProfiles'))

            .catch(error => console.log(error))
}

请注意,setState同时同步的,因此调用this.props.id(this.state.id);之后setState很可能会失败在第一次运行。

虽然以上应该解决眼前的问题,但我建议onAuthStateChangedhttps : //firebase.google.com/docs/auth/web/start#set_an_authentication_state_observer_and_get_user_data

这样,您可以登录并根据其值设置 Redux 状态,或者在用户刚刚返回页面时运行相同的代码来设置 Redux 值。它可能会导致比将所有东西都捆绑在一起更强大的情况signInWithEmailAndPassword

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 Firebase 和 Swift 分配、发送和检索用户特定的 ID 标签

FireBase Firestore通过ID列表检索多个文档

无法从 Firebase Firestore 检索数据

从 Firebase 检索用户 ID 下的数据

Firebase用户ID变量以检索数据

Firebase Firestore:获取生成的文档ID(Python)

通过ID查询Firebase Firestore文档

如何获取Firebase Firestore生成的ID

如何使用用户定义的文档ID创建Firebase Firestore文档?

在Cloud Functions for Firebase中从Firestore触发器获取用户ID?

哪种uid最适合用于Firebase Firestore用户文档ID?

在正确创建用户后,Firebase Firestore自定义ID无法创建新文档

在 Firebase 和 Android Studio 中检索司机的乘客请求节点内的所有乘客 ID(用户 ID)

从Firebase Firestore检索recyclerview中的图像

从 Firestore 中检索数据 - Firebase - Flutter

PHP可以从Firebase / Firestore检索数据吗?

在功能中从Firebase Firestore检索朋友的名字

从 firebase firestore 检索数据列表时出错

无法从 Cloud Firestore Firebase 检索数据

如何使用用户ID在Firebase中检索字段

firebase 检索用户 ID 以在 url API 调用中传递它

Flutter:关于检索 Firebase 用户 ID 的异步函数的问题

Firestore:從 Firebase 中按 id 查找元素

在Firebase Cloud Functions(Firestore)中获取文档ID

如何编写新文档的 ID [firebase.firestore().onSnapshot()]

如何过滤Firebase Cloud Firestore上的ID列表?

如何在Firebase Firestore中更新文档ID?

集合中所有ID的Firebase Firestore列表

Firebase Firestore 中带有子文档的 Python 列表 ID