3

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Vladimir Gabrielyan
  • 801
  • 1
  • 11
  • 23

1 Answers1

6

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.

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Just out of curiosity, why that information is not included in the user object, for me it would make sense that after `signInWithCustomToken` all custom fields would be available inside my user or auth() object. – Vladimir Gabrielyan Oct 07 '16 at 13:20