Where to find auth.token data, inside firebase objects

前端 未结 1 1393
误落风尘
误落风尘 2020-12-19 22:05

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(createCustomToke

相关标签:
1条回答
  • 2020-12-19 22:46

    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.

    0 讨论(0)
提交回复
热议问题