Stepan Kolomiyets
Stepan Kolomiyets

Reputation: 39

Firebase acces and id tokens

I'd like to know how to get both access and id tokens in Node.js SDK Firebase. When I print user object after signUpWithEmailAndPassword, I see that accessToken is one the properties there, but then when i use method on user object called getIdToken, I get the same token I saw in users object. Why then it is not called getAccessToken??? What I want is return to the client object containing access, id, refresh tokens and expiration time. P.S. I can't just say user.stsTokenManager.accessToken as it tells me that there is no already such property.

Upvotes: 3

Views: 4509

Answers (2)

Sanuj Bansal
Sanuj Bansal

Reputation: 115

const result = await getRedirectResult(auth);
if (result) {
  const provider = new FacebookAuthProvider();
  // This is the signed-in user
  const user = result.user;
  // This gives you a Access Token.
  const credential = provider.credentialFromResult(auth, result);
  const accessToken = credential.accessToken;
  // this gives you the id token
  const idToken = user.getIdToken();
}

Upvotes: 0

bojeil
bojeil

Reputation: 30868

This is only an internal name. This "accessToken" is really the Firebase ID token. You should rely on the officially supported getIdToken to get that Firebase ID token. Firebase also recently added getIdTokenResult which provides the ID token and additional information like expiration time and other token related information without you having to parse it from the ID token. You can also get the refreshToken from the user via firebase.auth().currentUser.refreshToken.

Upvotes: 6

Related Questions