Firebase Auth邮箱密码登录报错:TypeError: auth._getRecaptchaConfig is not a function
解决Firebase邮箱密码登录API的TypeError问题
错误根源
调用signInWithEmailAndPassword时参数顺序错误,该方法第一个参数必须是getAuth()返回的Auth实例,你直接传入邮箱和密码,导致Firebase内部调用依赖方法时抛出错误。
修正后的代码
import { initializeApp } from "firebase/app"; import { getAuth, signInWithEmailAndPassword } from "firebase/auth"; const firebaseConfig = { apiKey: "XXXXXXXXXXXXXX", authDomain: "XXXXXXXXXXXXXXXXXX", projectId: "XXXXXXXXXXXXXXXXXX", storageBucket: "XXXXXXXXXXXX", messagingSenderId: "XXXXXXXXXXX", appId: "XXXXXXXXX", }; const app = initializeApp(firebaseConfig); export default async function handler(req, res) { if (req.method === "GET") { return res.send("Working"); } else if (req.method === "POST") { const email = req.body.email; const password = req.body.password; const auth = getAuth(); // 修正参数顺序:先传入Auth实例,再传邮箱和密码 signInWithEmailAndPassword(auth, email, password) .then((userCredential) => { // signInWithEmailAndPassword返回的是UserCredential对象,用户信息需从user属性获取 const user = userCredential.user; return res.status(200).json({ user }); }) .catch((e) => { return res.status(500).json({ e: e.message || `${e}` }); }); } }
额外提示
- 务必严格遵循Firebase Auth方法的参数定义,避免参数顺序错误
- 捕获错误时优先返回
e.message,能得到更精准的错误描述 - 注意
signInWithEmailAndPassword的返回值结构,不要直接将返回结果当作用户对象处理
内容的提问来源于stack exchange,提问作者user20816962
相关产品推荐
相关产品推荐

