Reputation:
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 :)
Upvotes: 2
Views: 2449
Reputation: 6992
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
Upvotes: 1
Reputation: 317372
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.
Upvotes: 0