成功验证后,Flutter Firebase数据库权限被拒绝 [英] Flutter Firebase database permission denied after successful authentication

查看:95
本文介绍了成功验证后,Flutter Firebase数据库权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Flutter中使用firebase_auth和firebase_database插件. 使用电子邮件和密码进行身份验证. 这是用于身份验证和处理auth更改事件的代码提取.身份验证成功后,代码会将用户信息插入实时数据库.如果将数据库规则设置为读/写",则一切正常.但是,使用标准数据库写入规则(auth!= null),应用程序将抛出DatabaseError:权限被拒绝. 看来数据库插件不知道经过身份验证的身份? 任何想法有什么问题吗?

Using Flutter with plugins firebase_auth and firebase_database. Authenticating with email and password. This is code extract for authentication and handling auth change event. After auth success, code inserts user info into realtime database. Everything works fine if database rules are set to read/write true. But, with standard database write rules (auth != null), app throws DatabaseError: Permission denied. It appears that database plugin is not aware of authenticated identity? Any ideas what is wrong?

FirebaseAuth _auth = FirebaseAuth.instance;

user = await _auth.signInWithEmailAndPassword(
  email: _emailC.text,
  password: _passC.text
);

然后:

FirebaseAuth _auth = FirebaseAuth.instance;
StreamSubscription<FirebaseUser> _userChangeEvent;
DatabaseReference _usersDbRef = database.reference().child('users');

_userChangeEvent = _auth.onAuthStateChanged.listen((user) {
  setState(() {
    _isAuthenticated = user != null && user.isEmailVerified;
  });
  if (_isAuthenticated && _usersDbRef != null) {
    _usersDbRef.child(user.uid).once().then((
        DataSnapshot data) {
      if (data.value == null) {
        _usersDbRef.child(user.uid).set({
          'id': user.uid,
          'email': user.email,
          'display_name': user.displayName
        });
      }
    });
  }
});

我已经写过规则是标准的,但是由于安德烈要求在这里将它们包括在内,所以它们是: 这些都是一切正常的规则:

I already wrote that rules were standard, but since Andre asked to include them here they are: These are the rules when everything works fine:

{
  "rules": {
    ".read": "true || auth != null",
    ".write": "true || auth != null"
  }
}

以下是导致权限被拒绝"的规则:

These are the rules that result with "permission denied":

{
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}

推荐答案

在我发现问题仅在IOS上之后,我认为它的配置一定有问题,仅此而已. 根据此视频正确导入GoogleService-Info.plist后: https://youtu.be/3nFIMej3Tvw 可以正常工作很好.

After I found that issue is only on IOS, I figured it must be something wrong with my configuration and that was it. After correctly importing GoogleService-Info.plist based on this video: https://youtu.be/3nFIMej3Tvw it worked fine.

这篇关于成功验证后,Flutter Firebase数据库权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆