How to retrive additional fields from Firebase Custom Token into “UserInfo”

六眼飞鱼酱① 提交于 2019-12-12 11:20:17

问题


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

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