我正在使用管理SDK创建用户,我希望他们能够使用电子邮件和密码登录,但由于某些原因,当我通过客户端创建用户时,只使用电子邮件和密码,用户可以使用这些凭证登录,但当我使用管理SDK创建用户时,用户在auth仪表板中显示为匿名。出于某种原因,当我通过客户端只使用电子邮件和密码创建用户时,用户可以使用这些凭证登录,但当我使用管理员SDK创建用户时,该用户在auth仪表板中显示为匿名,并且用户不能使用他们的电子邮件和密码登录。客户端和Firebase端都没有显示错误。
如何使用管理员SDK创建一个Firebase用户,并让该用户链接到电子邮件认证?
Node:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.createUser = functions.https.onRequest(async (req, res) => {
//grab the email and password parameters
await admin.auth().createUser({
email: req.query.email,
password: req.query.password
})
//create the user
.then(function(userRecord) {
const child = userRecord.uid;
console.log('Successfully created new user:', userRecord.uid);
res.json({
status: 201,
data: {
"message": userRecord.uid
}
});
})
//handle errors
.catch(function(error) {
console.log();
res.json({
status: 500,
data: {
"error": 'error creating user: ', error
}
});
});
});
Swift:
func createChild(for parent: Parent,
with firstName: String,
lastName: String,
displayName: String?,
chores: [Chore]?,
username: String,
password: String,
completion: @escaping () -> Void = { }) {
let funcCallDict = [
"email": username,
"password": password
]
functions.httpsCallable(addChildIdentifier).call(funcCallDict) { (result, error) in
if let error = error {
NSLog("error: adding child with firebase function: \(error)")
completion()
return
}
}
completion()
}
你的功能是一个 HTTP类型触发:
exports.createUser = functions.https.onRequest
但你是想把它作为一个... ... 可调用型触发器:
functions.httpsCallable(addChildIdentifier).call(funcCallDict)
(请注意,可调用的触发器将被定义为与 onCall
,不 onRequest
.)
正如你从文档链接中看到的,它们不是一回事。 你可能是调用了HTTP触发器,但实际上并没有从客户端得到你所期望的参数,因为它们之间的协议是不同的。 试着记录一下 req.query.email
来理解我的意思。
你必须让你的函数成为一个合适的可调用函数,这样就可以在客户端使用所提供的库调用它,或者改变你在客户端调用它的方式,使用一个常规的http库而不是Firebase库。