通过 Firebase Cloud Functions 创建新用户 - 帐户已创建但无法登录



我是新来的。

我目前正在开发一个使用 Vue 的应用程序.js将 firebase auth、firebase 实时数据库和 firebase cloud 函数作为后端。应用程序必须包含的管理员帐户的一部分功能,该帐户可以为其他人创建帐户。创建后,新用户会收到一封电子邮件,其中包含登录名和登录密码。

由于注册方法(https://firebase.google.com/docs/auth/web/password-auth(会自动将用户重新登录到新创建的帐户,这显然不是想要的行为,因此我找到了一种通过云功能创建用户的方法。该代码在 Firebase 身份验证面板中成功创建了一个帐户,但我无法登录新创建的帐户。我收到一条消息:"密码无效或用户没有密码"。

此外,我不确定在这种情况下这是否意味着什么,但是使用云函数方法创建的帐户在Firebase身份验证面板中没有邮件图标(图片(。

云函数代码:

exports.createUser = functions.https.onCall((data, context) => {
console.log(data)
return admin.auth().createUser({
email: data.email,
emailVerified: true,
password: data.password,
displayName: data.email,
disabled: false,
})
.then(user => {
return {
response: user
}
})
.catch(error => {
throw new functions.https.HttpsError('failed to create a user')
});

}(;

登录代码:

signIn: async function(){
if(this.email && this.password){
let getUsers = firebase.functions().httpsCallable('getUsers')
this.feedback = null
this.spin = true
let destination = null
let logedUser = null
let type = null
this.feedback = 'Logging in...'
await firebase.auth().signInWithEmailAndPassword(this.email, this.password)
.then(response => {
this.feedback = 'Authorization finished...'
logedUser = response.user
})
.catch( error => {
this.feedback = error.message
this.spin = false
})
//... more code here but I am certain it has nothing to do with the problem.

由于 HTTPS 可调用函数的异步特性,使用当前代码时,您将尝试在通过云函数完全创建用户之前登录。

此外,您实际上并没有使用mailpassword参数调用云函数。

您应该根据文档执行以下操作。

....
let getUsers = firebase.functions().httpsCallable('getUsers')
await getUsers({email: this.email, password: this.password})
.then(result => {
return firebase.auth().signInWithEmailAndPassword(this.email, this.password)
})
.then(userCredential => {
this.feedback = 'Authorization finished...'
logedUser = userCredential.user
return true
})
.catch( error => {
this.feedback = error.message
this.spin = false
})
....

最新更新