request.auth.token.email not present in the rules

烂漫一生 提交于 2019-12-08 06:32:18

问题


Our app uses Firebase as the back end database. Firebase provides authentication for our app with google as the only provider. The authentication works fine. I am getting a "SUCCESS" in the authentication result. I would like to change my firebase rules so only the users that are present in a collection can access the data. For it, I have the rules in firebase to control access to the data. The rules are not working when I use any variables from request.auth.(where as the same works from a simulator)

Below rule working in both app and simulator:

match /restaurants/{$rId}/addresses/{document=**} {
    allow read, write : if get(/databases/$(database)/documents/users/abc@gmail.com).data.restaurantId == $rId;
}

Below rule NOT working from app but working in simulator

match /restaurants/{$rId}/addresses/{document=**} {
    allow read, write : if get(/databases/$(database)/documents/users/$(request.auth.token.email)).data.restaurantId == $rId;
}

I am sending the authentication request from the app as follows:

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

Handling the authentication result below:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RC_SIGN_IN) {
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        if (result.isSuccess()) {
             Toast.makeText(MyApplication.getAppContext(), "Login Successfull", Toast.LENGTH_LONG);
         }
    }
}

My firebase database contains the following structures

Users:

/users
   abc@gmail.com
            -restaurantId: "1" 

Restaurants:

/restaurants
   1
          addresses
                 local
                     -name: a
                 international
                     -name: b 

   2
          addresses
                 local
                     -name: c
                 international
                     -name: d 
   3
          addresses
                 local
                     -name: e
                 international
                     -name: f


I am able to see the Toast "Login successful" when the user tries to login with a valid password.

Should I save the authentication result in the onActivityResult so that further requests will have the request.auth.token.email present? Kinldy please guide me. Can anyone please let me know if I am doing anything wrong?

来源:https://stackoverflow.com/questions/57870178/request-auth-token-email-not-present-in-the-rules

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