Organize Firebase database for individual users, idea?

百般思念 提交于 2019-12-11 05:07:38

问题


In my android app, i had implemented google sign in with Firebase Auth procedures. As:

private void signIn() {
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, SIGNIN_CODE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == SIGNIN_CODE) {
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {
            // Authenicate with my Firebase
            GoogleSignInAccount account = task.getResult(ApiException.class);
            ProgressDialog dialog = new ProgressDialog(this); //ProgressDialog.show(this, null, "Please wait while we load sign in..", true);
            dialog.setMessage("Please wait while we load sign in..");
            dialog.setIndeterminate(true);
            Drawable drawable = new ProgressBar(this).getIndeterminateDrawable().mutate();
            drawable.setColorFilter(ContextCompat.getColor(this, R.color.progress),
                                    PorterDuff.Mode.SRC_IN);
            dialog.setIndeterminateDrawable(drawable);
            dialog.show();

            firebaseAuthWithGoogle(account);
        } catch (ApiException e) {
            // Google Sign In failed?
            Log.w(TAG, "Google sign in failed", e);
        }
    }
} //A lot more stuffs etc etc..

I already have a SQlite offline database. Now only for logged in users, i want the user can sync the SQlite database's contents with firebase database, which the user can access/sync later online.

But how to organize that single firebase database for individual signed in users with UIDs and etc.. ?

A little more specific advice/tutorial would be nice :)


回答1:


It can be done, you need to restrict access to data so that each user can access his/her data only by defining firebase security rules like below.

service cloud.firestore {
  match /databases/{database}/documents {
    match /yourrootnode/{uid} {
      allow read, write: if request.auth.uid == uid;
    }
}

Save user specific data from app like shown below, here dataModelObj object should contain uid field and you need to populate it from user object like dataModelObj .setUid(user.getUid());

  firestoreDB.collection("yourrootnode")
            .add(dataModelObj)
            .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {

.....

And then you can read only user data in app like this.

 firestoreDB.collection("yourrootnode")
                .whereEqualTo("uid", user.getUid())
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            ....
        } 

For more information you can check http://www.zoftino.com/android-firebase-email-password-authentication




回答2:


You can't make separate databases for each user, but you can organize your single database for multiple users. It's conventional to set aside a node of your database for users, and use their UID from Firebase Authentication to create a space for each user.

For example, the part of your database for users could be organized like this:

/users
    /uid1
        /name
    /uid2
        /name

You would then want to protect per-user data with user based security rules.



来源:https://stackoverflow.com/questions/48831813/organize-firebase-database-for-individual-users-idea

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