Flutter Firebase database permission denied after successful authentication

余生颓废 提交于 2019-12-11 00:55:45

问题


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
);

then:

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"
  }
}

回答1:


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.



来源:https://stackoverflow.com/questions/49245019/flutter-firebase-database-permission-denied-after-successful-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!