首页 > 解决方案 > Firebase:如何手动将 Google 登录身份验证器限制为特定的电子邮件地址?

问题描述

我如何才能将我的 React.js Web 应用程序上的 Google 登录仅用于特定的电子邮件地址?

我不希望限制某个域,而是告诉身份验证器只有特定的电子邮件地址可以注册或登录,并为所有其他人抛出错误。

谢谢。

标签: reactjsfirebase-authenticationgoogle-signin

解决方案


Firebase 身份验证中没有内置任何内容来限制登录到特定域或电子邮件地址。但是,如果您升级到 Google Cloud Identity(付费升级),您可以使用阻止的 Cloud Function 自定义身份验证流程

基于该链接中的示例:

// Import the Cloud Auth Admin module.
const gcipCloudFunctions = require('gcip-cloud-functions');
// Initialize the Auth client.
const authClient = new gcipCloudFunctions.Auth();
// Http trigger with Cloud Functions.
exports.beforeCreate = authClient.functions().beforeCreateHandler((user, context) => {
  // If the user is authenticating within a tenant context, the tenant ID can be determined from
  // user.tenantId or from context.resource, eg. 'projects/project-id/tenant/tenant-id-1'
  // Only this one user can sign up.
  if (user.email !== 'user@acme.com') {
    throw new gcipCloudFunctions.https.HttpsError('invalid-argument', `Unauthorized email "${user.email}"`);
});

推荐阅读