简体   繁体   English

Firebase 身份验证花费太长时间在云中创建用户 function

[英]Firebase authentication taking too long to create user in cloud function

currently I'm having a problem where calling the auth#createUser method takes up to 10 seconds to proceed to call it's Promise#then method.目前我遇到一个问题,调用 auth#createUser 方法最多需要 10 秒才能继续调用它的Promise#then方法。 I'm getting these timestamps from the firebase & google cloud function logger.我从 firebase 和谷歌云 function 记录器获取这些时间戳。 I feel that I may be doing something wrong, although I can't figure out what I am doing wrong.我觉得我可能做错了什么,虽然我无法弄清楚我做错了什么。 Before you say to contact the firebase support, I already have and they told me to come here.在您说要联系 firebase 支持之前,我已经有了,他们告诉我来这里。


const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

let database = admin.firestore();

exports.createUser = functions.https.onCall((data, response) => {
    console.log(data);
    console.log(response);

    admin.auth().createUser({
        email: data.email,
        emailVerified: false,
        password: data.password,
        displayName: data.name,
        disabled: false,
    }).then(user => {
        database.collection('users').doc(data.username).set({
            uid: user.uid,
            email: data.email,
            name: data.name,
            username: data.username
        }).catch(error => {
            throw new functions.https.HttpsError(error)
        });
        console.log('The entire thing is done successfully!');
        return {
            response: user
        }
    }).catch(error => {
        throw new functions.https.HttpsError(error);
    });
    console.log('Found my way to the end of the method');
});

You're not dealing with promises correctly.你没有正确处理承诺。 onCall functions should return a promise that resolves with the data that you want to return to the client. onCall 函数应该返回一个 promise,它使用您想要返回给客户端的数据进行解析。 Right now, your function is returning nothing.现在,您的 function 没有返回任何内容。 The return statement inside the then callback isn't actually sending anything to the client. then回调中的 return 语句实际上并没有向客户端发送任何内容。 What you will need to do instead is return the promise chain:您需要做的是返回 promise 链:

return admin.auth().createUser(...).then(...).catch(...)

Note the return in front of all that.请注意所有这些前面的返回。

Also, you will need to handle the promise returned by set() .此外,您将需要处理set()返回的 promise 。 Just calling catch isn't sufficient.仅仅调用catch是不够的。 You will need to return that promise as well from the then callback.您还需要从then回调中返回 promise。

I strongly suggest learning how promises work in JavaScript - without proper handling, your functions will simply not work correct, and often behave in confusing ways.我强烈建议学习 Promise 在 JavaScript 中是如何工作的——如果没有适当的处理,你的函数将根本无法正常工作,并且经常以令人困惑的方式运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM