我有一个客户想要访问 Firebase 控制台,以便他们可以在身份验证模块中手动添加用户。
我尝试通过“用户和权限”添加它们,但找不到任何适合在身份验证中添加用户且在数据库中没有写入权限的角色。
目前我将它们添加为项目编辑器,但对它并不满意。
我有一个客户想要访问 Firebase 控制台,以便他们可以在身份验证模块中手动添加用户。
我尝试通过“用户和权限”添加它们,但找不到任何适合在身份验证中添加用户且在数据库中没有写入权限的角色。
目前我将它们添加为项目编辑器,但对它并不满意。
授予管理员访问您的应用仪表板的权限可能不是管理应用内用户的正确答案。它甚至可能是一个安全风险。在我看来,这相当于让您的应用程序用户通过 shell 提示访问您的物理服务器,而不是创建一个 API 供他们调用。
一个更好的选择是设置一个 Google Cloud Functions 端点,该端点将接受 API 请求并代表他们创建用户,通过您确定的某些标准验证他们的访问权限。
1)启用和部署云功能
2) 设置经过身份验证的 HTTPS 端点
3) 创建新用户的函数代码如下所示:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const express = require('express');
const cookieParser = require('cookie-parser')();
const cors = require('cors')({origin: true});
const app = express();
// See https://github.com/firebase/functions-samples/blob/Node-8/authorized-https-endpoint/functions/index.js
const validateFirebaseIdToken = require('./validateFirebaseIdToken');
app.use(cors);
app.use(cookieParser);
app.use(validateFirebaseIdToken);
app.get('/createUser', (req, res) => {
const userData = req.params;
// This represents some criteria you set for determining who can call this endpoint
// possible a list of approved uids in your database?
if( req.user.uid !== VALID_ADMIN_USER ) {
res.status(401).send('Unauthorized');
return;
}
// See https://firebase.google.com/docs/auth/admin/manage-users#create_a_user
admin.auth().createUser({
email: userData.email,
displayName: userData.name,
...
})
.then(function(userRecord) {
// See the UserRecord reference doc for the contents of userRecord.
res.json({result: 'success', uid: userRecord.uid});
console.log("Successfully created new user:", userRecord.uid);
})
.catch(function(error) {
console.error("Failed to create new user");
console.error(error);
res.status(500).json({status: 'error', error: 'Unable to process the request'});
});
});
// This HTTPS endpoint can only be accessed by your Firebase Users.
// Requests need to be authorized by providing an `Authorization` HTTP header
// with value `Bearer <Firebase ID Token>`.
exports.app = functions.https.onRequest(app);
4) 向您的客户端提供 API 端点或构建一个基本的应用程序/Web 界面,他们可以使用该界面调用此端点。
因此,请转到 Google Cloud Platform(从 Firebase 控制台),然后选择 Manage Roles from where you can create Custom roles
。
请注意,自定义角色目前处于测试阶段,您可能无法实现您需要的功能,但正如文档所建议的那样:
自定义角色允许您对权限进行分组并将其分配给您的项目或组织的成员。您可以手动选择权限或从其他角色导入权限。