我正在学习 firebase,我检查了本教程有关身份验证的内容。 (https://www.youtube.com/watch?v=rbuSx1yEgV8&t=502s)。在这个视频中,模拟器似乎是必不可少的,但我想与服务器进行通信。我该怎么做?如果我不初始化身份验证模拟器(通过删除 connectEmulator() 函数),我只会收到错误“auth/network-request-failed”。
const firebaseConfig = {
//...
};
const user = {
email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="483d3b2d3a083c2d3b3c66252d" rel="noreferrer noopener nofollow">[email protected]</a>',
password: 'test1234'
}
function func() {
createUserWithEmailAndPassword(auth, user.email, user.password)
.then((userCredential) => {
const user = userCredential.user;
console.log(user)
})
.catch((error) => {
console.log(error)
// ..
});
}
正如您从该视频的 7:37 分钟看到的,我明白了他的问题!所以我认为我遵循了错误的方法。有人能帮我吗?我将不胜感激。
最佳答案
您应该能够通过服务器进行身份验证。
模拟器是可选的。就我个人而言,我很少使用它,并且基本上总是使用真正的在线 Firebase 服务器。但是,在您能够通过服务器进行身份验证之前,还有许多步骤。
第 1 步。检查您是否已正确复制配置
转到此链接,但将 PROJECT_ID 替换为您的实际项目 ID:
https://console.firebase.google.com/u/0/project/PROJECT_ID/settings/general/
检查您是否已将其值从该页面正确复制到您的应用代码中。如果您尚未“添加应用程序”,则可能需要单击“添加应用程序”才能显示此配置。
const firebaseConfig = {
... blah blah ...
};
第 2 步:检查您是否已启用“登录提供商”
转到此链接(同样,PROJECT_ID 应替换为您的项目 ID):
https://console.firebase.google.com/u/0/project/PROJECT_ID/authentication/providers
至少需要打开其中一个提供程序,如下所示:
第 3 步。您的代码看起来不错。
我假设您已正确设置auth
- 我们在上面的代码片段中看不到这一点。
请将您在控制台上看到的确切错误消息以文本形式粘贴到您的问题中。
您可能希望按如下方式加强调试:
function func() {
console.log(`user: ${JSON.stringify(user,null,2)}`)
createUserWithEmailAndPassword(auth, user.email, user.password)
.then((userCredential) => {
console.log(`userCredential.user: ${JSON.stringify(userCredential.user,null,2)}`)
})
.catch((error) => {
console.error(error)
});
}
这是一件小事,但我建议避免对两个不同的事物使用相同的变量名称,user
。 Javascript 会将它们分开,但我们作为程序员有时在回顾代码时会感到困惑。
第 4 步。确保您已授权您正在使用的域。
转到:
https://console.firebase.google.com/u/0/project/PROJECT_ID/authentication/settings
确保您已授权您“调用”Firebase 服务器的域。
如果您的应用程序在“127.0.0.1”而不是“localhost”上运行,您可能还需要添加该 IP 地址。或者,如果您已部署,则为已部署的域。
关于javascript - 如何在没有模拟器的情况下使用 firebase?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75042468/