如何在 Firebase 3 中创建一个用户而不对其进行身份验证?
How to just create an user in Firebase 3 and do not authenticate it?
我正在做一个 angularfire 项目,我想知道如何在 Firebase 3 中创建一个用户,并且一旦完成,不验证指定的用户。在之前的 Firebase 版本中,我们有一个名为 createUser(email, password) 的方法。现在我们只有方法 createUserWithEmailAndPassword(email, password),它创建并验证指定的用户。
问题的答案是:你不能。
我们有类似的情况,我们有 'admin' 可以创建其他用户的用户。使用 2.x 这很容易。使用 3.x 这是一个失败,因为该功能已被完全删除。
如果您在 3.x 中创建用户,您将以该用户的身份进行身份验证,并取消对已登录帐户的身份验证。
这会更深入,因为您需要重新验证才能创建另一个用户;所以管理员要么手动执行此操作,要么(畏缩)在本地存储身份验证数据,以便它可以是一个自动化过程(畏缩,请不要这样做)
Firebase 已公开强调 2.x 将继续得到支持,因此您可能只想避免使用 3.x。
更新:
其中一个 Firebaser 实际上想出了一个解决方法。从概念上讲,您有一个管理员用户登录。然后您创建第二个连接到 firebase 并与另一个用户进行身份验证,然后该连接创建新用户。冲洗 - 重复。
再次更新
See this question and answer
您可以使用云函数和 firebase admin SDK 执行此操作。
像下面这样创建 HTTP 函数。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions
exports.createUser = functions.https.onRequest((request, response) => {
if (request.method !== "POST") {
response.status(405).send("Method Not Allowed");
} else {
let body = request.body;
const email = body.email;
const password = body.password;
const displayName = body.displayName;
admin.auth().createUser({
email: email,
emailVerified: false,
password: password,
displayName: displayName,
disabled: false
})
.then((userRecord) => {
return response.status(200).send("Successfully created new user: " +userRecord.uid);
})
.catch((error) => {
return response.status(400).send("Failed to create user: " + error);
});
}
});
在您的客户端应用程序中,使用 Http 请求调用此函数,例如使用 ajax
$.ajax({
url: "the url generated by cloud function",
type: "POST",
data: {
email: email,
password: password,
displayName: name
},
success: function(response) {
console.log(response);
},
error: function(xhr, status, error) {
let err = JSON.parse(xhr.responseText);
console.log(err.Message);
}
});
我正在做一个 angularfire 项目,我想知道如何在 Firebase 3 中创建一个用户,并且一旦完成,不验证指定的用户。在之前的 Firebase 版本中,我们有一个名为 createUser(email, password) 的方法。现在我们只有方法 createUserWithEmailAndPassword(email, password),它创建并验证指定的用户。
问题的答案是:你不能。
我们有类似的情况,我们有 'admin' 可以创建其他用户的用户。使用 2.x 这很容易。使用 3.x 这是一个失败,因为该功能已被完全删除。
如果您在 3.x 中创建用户,您将以该用户的身份进行身份验证,并取消对已登录帐户的身份验证。
这会更深入,因为您需要重新验证才能创建另一个用户;所以管理员要么手动执行此操作,要么(畏缩)在本地存储身份验证数据,以便它可以是一个自动化过程(畏缩,请不要这样做)
Firebase 已公开强调 2.x 将继续得到支持,因此您可能只想避免使用 3.x。
更新:
其中一个 Firebaser 实际上想出了一个解决方法。从概念上讲,您有一个管理员用户登录。然后您创建第二个连接到 firebase 并与另一个用户进行身份验证,然后该连接创建新用户。冲洗 - 重复。
再次更新
See this question and answer
您可以使用云函数和 firebase admin SDK 执行此操作。 像下面这样创建 HTTP 函数。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions
exports.createUser = functions.https.onRequest((request, response) => {
if (request.method !== "POST") {
response.status(405).send("Method Not Allowed");
} else {
let body = request.body;
const email = body.email;
const password = body.password;
const displayName = body.displayName;
admin.auth().createUser({
email: email,
emailVerified: false,
password: password,
displayName: displayName,
disabled: false
})
.then((userRecord) => {
return response.status(200).send("Successfully created new user: " +userRecord.uid);
})
.catch((error) => {
return response.status(400).send("Failed to create user: " + error);
});
}
});
在您的客户端应用程序中,使用 Http 请求调用此函数,例如使用 ajax
$.ajax({
url: "the url generated by cloud function",
type: "POST",
data: {
email: email,
password: password,
displayName: name
},
success: function(response) {
console.log(response);
},
error: function(xhr, status, error) {
let err = JSON.parse(xhr.responseText);
console.log(err.Message);
}
});