首页 > 解决方案 > 如何将令牌和用户身份验证存储在 firestore ..?

问题描述

我想将用户的令牌和电子邮件、密码存储在 firestore。如果我将代码放入 js 文件并登录,就会创建 firestore 集合……对吧?

但是我可以从控制台获取一些消息……而不是 Firestore 数据。

在我的控制台中......有消息,1.“获得许可”2.解释我的服务人员 3.“令牌”

这是我的代码的一部分。firebase.init.js

firebase.initializeApp(firebaseConfig);


const messaging = firebase.messaging();


const loginUser = ({
    email,
    password
}) => {
    return (dispatch) => {
        // dispatch action
        dispatch({
            type: LOGIN_USER
        })
        .catch(function(error) {
        console.log('error occured.')
        });
        auth.createUserWithEmailAndPassword(email, password)     **changed
        .then((userCredential) => {
            // Signed in 
            var user = userCredential.user;
            // ...
        })
        .catch((error1) => {
            var errorCode = error1.code;
            var errorMessage = error1.message;
            console.log(errorCode, errorMessage);
            // ..
        });
    };
};
messaging.requestPermission()
    .then(function() {
        console.log('got the permission');
        // get the device's push token
        return messaging.getToken()
    })
    .then(function(token) {
        const { currentUser } = auth();
        token => {
            console.log(currentUser.email, currentUser.uid)
                .catch(function(error2) {
                console.log('error2 occured.')
            });
            // store token in the user's document
            db.collection('users').doc(currentUser.uid)
                .set({
                    pushToken: token,
                    numFinds: 0,
                    numCares: 0
                })
                .catch(function(error3) {
                console.log('error3 occured.' );
            });
        };
        console.log(token);
    })
    .catch(function(error4) {
        console.log('error4 occured.');
    });

谢谢你的帮助。

标签: javascriptgoogle-cloud-firestorefirebase-cloud-messaging

解决方案


我的理解是您没有正确使用该功能createUserWithEmailAndPassword,请抓住该功能的错误,因为它可能会揭示问题。

来自 [1]

firebase.auth().createUserWithEmailAndPassword(email, password)
  .then((userCredential) => {
    // Signed in 
    var user = userCredential.user;
    // ...
  })
  .catch((error) => {
    var errorCode = error.code;
    var errorMessage = error.message;
    // ..
  });

[1] https://firebase.google.com/docs/auth/web/password-auth#create_a_password-based_account


推荐阅读