在firebase对象里面找到auth.token数据的位置

时间:2016-10-07 12:13:12

标签: firebase firebase-authentication firebase-security

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

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

1 个答案:

答案 0 :(得分:5)

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

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));
});

解析JWT的功能来自这个问题:How to decode jwt token in javascript

您注意到它并未验证ID令牌是否有效。这对我来说在客户端代码中似乎很好,因为无论如何信息都将由用户自己使用。但是,如果您确实要验证令牌,则必须使用更复杂的方法。