Search code examples
flutterfirebasefirebase-realtime-databasefirebase-authenticationflutterflow

Firebase realtime database auth in flutter


I'm using Firebase realtime database and flutter to create an app. I am following https://firebase.google.com/docs/database/flutter/start. Using the following code:

    final firebaseApp = Firebase.app();
    final database = FirebaseDatabase.instanceFor(
        app: firebaseApp,
        databaseURL: 'https://myproject.firebaseio.com/');

I can access a document that has ".read": true. When I try to access a document where ".read": "auth != null", I get a permission denied error.

How can I use RTDB with Firebase Auth?


Solution

  • The Firebase SDKs all work together, so once you sign a user in (for example with email/password) to Firebase Authentication the SDK for the Realtime Database will automatically pick that up, pass the information to the server, which then makes it available in the auth variable of your security rules.

    From the documentation on how Firebase Authentication works:

    After a successful sign in, you can access the user's basic profile information, and you can control the user's access to data stored in other Firebase products.

    The easiest way to connect and configure everything is by following the flow in the documentation on adding Firebase to your Flutter app, which uses the flutterfire CLI tool that @BGPark also commented on.