为个人用户组织Firebase数据库,对吗? [英] Organize Firebase database for individual users, idea?

查看:63
本文介绍了为个人用户组织Firebase数据库,对吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用中,我已经使用Firebase Auth程序实现了google登录. 为:

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..

我已经有一个SQlite离线数据库.现在仅适用于已登录的用户,我希望用户可以将SQlite数据库的内容与firebase数据库同步,用户以后可以在线访问/同步.

但是如何为具有UID等的个人登录用户组织单个Firebase数据库??

更具体的建议/教程会很不错:)

解决方案

可以做到,您需要限制对数据的访问,以便每个用户只能通过定义如下所示的Firebase安全规则来访问其数据.

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

从应用程序中保存用户特定的数据,如下所示,此处dataModelObj对象应包含uid字段,您需要从用户对象中填充数据,例如dataModelObj .setUid(user.getUid());

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

.....

这样您就只能在应用程序中读取用户数据.

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

有关更多信息,您可以检查 http://www.zoftino.com /android-firebase-email-password-authentication

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

解决方案

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

这篇关于为个人用户组织Firebase数据库,对吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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