firebase - 防止在 firestore 中通过电子邮件登录创建用户帐户

标签 firebase firebase-authentication angularfire

我已经使用 firebase 通过电子邮件链接登录设置了无密码登录。一切都按预期进行,但是当用户收到电子邮件并单击链接登录时,他们会自动创建为注册用户。

https://firebase.google.com/docs/auth/web/email-link-auth

After a user signs in for the first time, a new user account is created and linked to the credentials...

这意味着任何在登录屏幕中提出请求的人都将收到一封电子邮件并可以访问该网站。

我不确定是否需要完成任何配置或设置,以便要求仅针对已注册的用户检查请求注册链接的用户。

类似于

 firebase.auth().sendLoginLinkToEmail(email,{url:...,handleCodeInApp:true}).then(() =>{
    ....
  }, error =>{
     // return if not an authenticated user
  })

如果电子邮件未注册,则会返回错误。

这个想法是让管理员创建用户,然后这些创建的用户只需使用电子邮件链接登录(无密码)

这可能吗?阻止 firebase 使用.signInWithEmailLink() 创建帐户?

最佳答案

无密码电子邮件登录允许用户证明他们有权访问某个邮箱。它本质上并没有做更多的事情。用户单击链接后,他们就会通过身份验证。除了启用/禁用整个登录提供程序之外,您无法控制谁可以登录/进行身份验证。

之后,由您的应用程序决定允许该用户执行哪些操作。这是一个单独的步骤,通常称为授权

Firebase 身份验证(顾名思义)仅负责身份验证。您必须在其他地方处理授权,具体取决于您为用户提供访问权限的服务。

是什么让电子邮件在您的应用中“注册”? IE。管理员在哪里创建这些用户?例如,如果您将用户存储在 Cloud Firestore 中在集合 allowed_users 中,文档如下:

allowed_users: // collection
  "arkade@domain,com": { ... } // document
  "puf@domain,com": { ... } // document

现在,您可以使用 Firestore 的服务器端安全规则限制只有允许的用户才能访问其他数据。假设您有一组帖子,您可以仅允许这些用户通过以下方式阅读帖子:

service cloud.firestore {
  match /databases/{database}/documents {
    match /posts/{post} {
      // Make sure a 'allowed_users' document exists for the requesting user before
      // allowing any reads from the 'posts' collection
      allow read: if exists(/databases/$(database)/documents/allowed_users/$(request.auth.email))
  }
}

语法有点长,但您可以看到,仅当当前用户的电子邮件地址 (request.auth.email) 作为文档存在于 中时才允许阅读帖子>allowed_users

在 Firestore 规则的规则版本 2 中,您访问当前用户的电子邮件地址的方式略有不同。您可以通过 request.auth.token.email 完成此操作。下面的示例还展示了如何在当前用户的文档中获取 bool 属性(如果您通过电子邮件识别该用户):

allow write: if get(/databases/$(database)/documents/users/$(request.auth.token.email)).data.admin == true;

关于firebase - 防止在 firestore 中通过电子邮件登录创建用户帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52792228/

相关文章:

javascript - Firebase 什么时候开始从数据库中提取数据?

ios - Firebase Cloud Messaging 是否支持 VOIP pushkit 服务?

firebase - Firestore 安全规则获取功能错误

ios - Firebase Swift iOS 取回图像时的图像缓存问题

firebase - AngularFire Loop 非规范化数据

firebase - 错误: reCAPTCHA container is either not found or already contains inner elements

java - 将特定子项获取到 Firebase RecyclerView

reactjs - 类型错误 : this is undefined while setting state in reactjs

angularjs - Angular : ng-bind-html removing ng directives from HTML data fetched from Firebase

Ionic 2 应用程序中的 Firebase 身份验证网络错误