简体   繁体   English

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

[英]Can I validate email's domain zone in Firebase Authentication?

Can I validate email domain zone in Firebase Authentication? 我可以在Firebase身份验证中验证电子邮件域区域吗?

For example, I want to give success registration just for an email from yahoo and gmail ( @yahoo.com, @gmail.com emails) 例如,我只想为来自yahoo和gmail的电子邮件 @ yahoo.com, @ gmail.com电子邮件)进行成功注册。

ps of course I can validate it in client side, but this isn't enough ps当然我可以在客户端验证它,但这还不够

Unfortunately, you can not validate the email's domain before registration (exclude the client side). 不幸的是,您无法在注册之前验证电子邮件的域(不包括客户端)。

There are some options in front of you, that you can do: 您可以执行一些选择:

  • Option 1: To prevent access to the database and storage if the user's domain is not some of your specific domains: 选项1:如果用户的域不是您的某些特定域,则防止访问数据库和存储:

For example: 例如:

"rules": {
    ".read": "auth.token.email.endsWith('@gmail.com')",
    ".write": "auth.token.email.endsWith('@gmail.com')"
  }
}

or like this: 或像这样:

"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$/)"
  }
}

Credits: https://stackoverflow.com/a/38019847/2765346 积分: https : //stackoverflow.com/a/38019847/2765346

  • Option 2: To add a Firebase Authentication trigger and listen for new users. 选项2:添加Firebase身份验证触发器并侦听新用户。 Then you can validate the new registered users and disable these with invalid domains: 然后,您可以验证新注册的用户并使用无效的域禁用它们:

For example: 例如:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM