我可以在 Firebase 身份验证中验证电子邮件的域区域吗?

Art*_*sky 3 javascript firebase firebase-authentication angularfire2

我可以在 Firebase 身份验证中验证电子邮件域区域吗?

例如,我只想为来自 yahoo 和 gmail 的电子邮件 @yahoo.com、@gmail.com 电子邮件)提供成功注册

ps当然我可以在客户端验证它,但这还不够

Hri*_*mov 5

不幸的是,您无法在注册前验证电子邮件的域名(不包括客户端)。

您可以选择以下一些选项:

  • 选项 1:如果用户的域不是您的某些特定域,则要阻止访问数据库和存储:

例如:

"rules": {
    ".read": "auth.token.email.endsWith('@gmail.com')",
    ".write": "auth.token.email.endsWith('@gmail.com')"
  }
}
Run Code Online (Sandbox Code Playgroud)

或者像这样:

"rules": {
    ".read": "auth.token.email_verified == true && auth.token.email.matches(/.*@gmail.com$/)",
    ".write": "auth.token.email_verified == true && auth.token.email.matches(/.*@gmail.com$/)"
  }
}
Run Code Online (Sandbox Code Playgroud)

学分:/sf/answers/2661389321/

  • 选项 2:添加 Firebase 身份验证触发器并侦听新用户。然后您可以验证新注册用户并禁用无效域的用户:

例如:

exports.validateUser = functions.auth.user().onCreate((user) => {
   if (!user.email.matches(/.*@gmail.com$/)) {
       admin.auth().updateUser(data, {
           disabled: true
       });
   }
});
Run Code Online (Sandbox Code Playgroud)

学分: https: //firebase.google.com/docs/functions/auth-events