如何从Firebase自定义标记将其他字段转换为“UserInfo” [英] How to retrive additional fields from Firebase Custom Token into "UserInfo"

查看:118
本文介绍了如何从Firebase自定义标记将其他字段转换为“UserInfo”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自定义标记方法在Firebase中进行身份验证。
从服务器,我得到JWToken除了其他标记字段之外还有以下数据:
$ b $ $ p $ {uid :user_uid,user_role:admin}



我们用来获取标记字段为 -

 <$ c $ 。C> authData.getAuth()得到( USER_ROLE)的toString(); 

但在新的Firebase SDK中:

如何从标记中获取数据(user_role的值)。

lockquote

注意:使用 UserInfo 获取user_role的值,
,但是没有方法/方法来获取这样的自定义字段。

$ b $在实时数据库和存储的Firebase安全规则中,可以访问其他声明。

在客户端,由于JWT声明只是编码到令牌中,因此您可以解码令牌并查看声明。您可以使用jwt解码库在浏览器上执行此操作。解码后,您将能够看到所有的自定义auth声明。

您也可以将声明存储在个人用户节点下的Realtime数据库本身中,然后只读它需要时从那里。然后,您可以为该节点设置一个规则,以便只有登录的用户才能读取他们自己的声明。


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.

解决方案

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.

这篇关于如何从Firebase自定义标记将其他字段转换为“UserInfo”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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