gpt4 book ai didi

javascript - Firebase 身份验证异步等待

转载 作者:行者123 更新时间:2023-12-02 16:50:50 24 4
gpt4 key购买 nike

我刚开始使用异步等待,我正在尝试在 firebase 中使用 Auth createUserWithEmailAndPassword

注册

exports.signup = async (req, res) => {
const { email, password, confirmPassword, handle } = req.body

const newUser = {
email,
password,
confirmPassword,
handle
}

try {
const response = await firebase.auth().createUserWithEmailAndPassword(newUser.email, newUser.password)
const token = response.getIdToken()
console.log('THIS IS THE RESPONSE', token)
// return token
return res.status(200).json({
message: 'User Successfully Added!',
token: token
})

} catch (err) {
if (err.code === 'auth/email-already-in-use') {
return res.status(400).json({
message: 'Email already taken!'
})
} else {
return res.status(500).json({
message: 'Something went wrong, Please try again later'
})
}
}
}

我的问题是,这实际上是在创建一个帐户,但总是返回 500 出现错误,请稍后重试

编辑:

console.log(err) 给出以下输出:

TypeError: response.getIdToken is not a function

我会尝试调查一下。

最佳答案

createUserWithEmailAndPassword 返回 Promise< UserCredential >getIdToken 是用户的方法 ( Documentation )

const response = await firebase.auth().createUserWithEmailAndPassword(newUser.email, newUser.password);
const token = await response.user.getIdToken(); // getIdToken is a method of user
console.log('THIS IS THE RESPONSE', token);
// return token
return res.status(200).json({
message: 'User Successfully Added!',
token: token
});

关于javascript - Firebase 身份验证异步等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58794296/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com