Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Firebase how to save and retrieve data under specific user

i am trying to integrate my app with firebase to save simple data on cloud.

Example:

user open the app and login.

user write some stuff,the data saved on cloud.

when the user will use the app again he will see his data.

i have read the docs but i coud not find any example how the structure works between the user and the data. user logged in , now how to save strings/object for that user?

what i tried:

user login or authenticate the user

Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.addAuthStateListener(new Firebase.AuthStateListener() {
    @Override
    public void onAuthStateChanged(AuthData authData) {
        if (authData != null) {
            // user is logged in
        } else {
            // user is not logged in
        }
    }
});

now how to save under that user objects/strings?

like image 939
Eli Avatar asked Jan 29 '26 15:01

Eli


1 Answers

It looks like you're using and old Android API for Firebase. With the latest Android API there is no more need for "https://<YOUR-FIREBASE-APP>.firebaseio.com" in your code. I would suggest to migrate, using that migration guide.

Once you have logged your user following that guide

Then you can store user information like in that documentation :

String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
mDatabase.child("users").child(userId).child("name").setValue("John");

Don't forget to change your security rule in Firebase to make sure only this user can access his own data: guide here. It's very easy and quick to do and very important for keeping your user's data private.

like image 133
Sistr Avatar answered Jan 31 '26 05:01

Sistr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!