Vladimir Gabrielyan
Vladimir Gabrielyan

Reputation: 801

Where to find auth.token data, inside firebase objects

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.

Upvotes: 3

Views: 1699

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600006

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.

Upvotes: 6

Related Questions