我不熟悉使用异步等待,并且正在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不是函数
我会尽力研究它。
答案 0 :(得分:1)
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
});