问题
I am using custom token method to authenticate in firebase. From server, I am getting JWToken which has following data in addition to other token fields:
{ "uid" : "user_uid", "user_role": "admin" }
In Firebase 2.x:
We used to fetch token fields as -
authData.getAuth().get("user_role").toString();
But In the new Firebase SDK:
How to get the data (value of "user_role") from token.
NOTE: I have tried using UserInfo to get the value of "user_role", but there is no method/way to fetch such custom fields.
回答1:
the additional claims are accessible in the Firebase security Rules for the Realtime Database and Storage.
On the client side, since the JWT claims are just encoded into the token, you can decode the token and see the claims. You can use jwt-decode library to do this on the browser. After decoding, you will be able to see all the custom auth claims.
You can also store the claims in the Realtime database itself under the individual users' node and just read it from there when needed. You can then set a rule for that node such that only the logged in user can read their own claims.
来源:https://stackoverflow.com/questions/38246371/how-to-retrive-additional-fields-from-firebase-custom-token-into-userinfo