我如何在 firestore 中存储令牌和用户身份验证..?

时间:2021-02-01 20:20:55

标签: javascript google-cloud-firestore firebase-cloud-messaging

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

但我可以从控制台获取一些消息...不是 firestore 数据。

在我的控制台中...有消息, 1.'得到许可' 2. 解释我的 serviceworker 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.');
    });

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我的理解是您没有正确使用函数 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