Seungsik Shin
Seungsik Shin

Reputation: 205

getIdToken() is not a function, even if It is used like a function in firebase document

I am dealing with firebase auth now and I was following this Firebase document.

Visit https://firebase.google.com/docs/auth/admin/manage-cookies#sign_in

// When the user signs in with email and password.
firebase.auth().signInWithEmailAndPassword('[email protected]', 'password').then(user => {
  // Get the user's ID token as it is needed to exchange for a session cookie.
  return user.getIdToken().then(idToken = > {
    // Session login endpoint is queried and the session cookie is set.
    // CSRF protection should be taken into account.
    // ...
    const csrfToken = getCookie('csrfToken')
    return postIdTokenToSessionLogin('/sessionLogin', idToken, csrfToken);
  });
})

I expected that I could get a token by using that function. But It doesn't work because user in the code doesn't have getIdToken() function.

Upvotes: 12

Views: 12702

Answers (2)

Footniko
Footniko

Reputation: 2752

Seems like things changed since 7.* version. To get it:

firebase.auth().signInWithEmailAndPassword('[email protected]', 'password').then(({ user }) => {
  // Get the user's ID token as it is needed to exchange for a session cookie.
  return user.getIdToken().then(idToken = > {
    // Session login endpoint is queried and the session cookie is set.
    // CSRF protection should be taken into account.
    // ...
    const csrfToken = getCookie('csrfToken')
    return postIdTokenToSessionLogin('/sessionLogin', idToken, csrfToken);
  });
})

Note, that you need to use user.user.getIdToken() now, or just use destructuring as I did in the example.

Upvotes: 11

Federkun
Federkun

Reputation: 36989

To get the id token, just call auth's currentUser#getIdToken directly.

const idToken = await firebase.auth().currentUser.getIdToken()

Upvotes: 10

Related Questions