在哪里可以找到auth.token数据,在firebase对象内部 [英] Where to find auth.token data, inside firebase objects

查看:391
本文介绍了在哪里可以找到auth.token数据,在firebase对象内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 signInWithCustomToken ,在身份验证后,我找不到存储在服务器端设置的自定义声明数据(createCustomToken)。



我可以通过auth.token在firebase规则中看到它们,但是如何通过我的javascript代码中的firebase对象访问它们。

解决方案

令牌中的信息不会自动提供给您的应用程序代码。但它被嵌入到令牌中,所以你可以自己解码:

pre $ function $ pa $ J $($) base64Url = token.split('。')[1];
var base64 = base64Url.replace(' - ','+')。replace('_','/');
返回JSON.parse(window.atob(base64));
};

var user = firebase.auth()。currentUser
user.getToken()。then(data => {
console.log(parseJwt(data));
});

解析JWT的函数来自这个问题:



如何在javascript中解码jwt token?请注意,它不验证ID令牌是否有效。这在客户端代码中似乎很好,因为信息将由用户自己使用。但是,如果您确实想要验证令牌,则必须使用更复杂的方法。


I am using signInWithCustomToken, after authentication I can not find where is stored my custom claims data which I have set in the server side(createCustomToken).

I can see them in firebase rules via auth.token, but how can I access them through firebase objects from within my javascript code.

解决方案

The information in the token is not automatically available to your application code. But it is embedded in the token, so you can decode it yourself:

function parseJwt (token) {
    var base64Url = token.split('.')[1];
    var base64 = base64Url.replace('-', '+').replace('_', '/');
    return JSON.parse(window.atob(base64));
};

var user = firebase.auth().currentUser
user.getToken().then(data => {
    console.log(parseJwt(data));
});

The function to parse the JWT comes from this question: How to decode jwt token in javascript

You'll note that it doesn't verify that the ID token is valid. That seems fine to me in client-side code, since the information will be used by the user themselves anyway. But if you do want to verify the token, you'll have to use a more involved method.

这篇关于在哪里可以找到auth.token数据,在firebase对象内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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