Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to retrieve a user firebase auth ID token in the new firebase version 9?

How to retrieve a user firebase auth ID token in the new firebase version 9?

This is how it is done in firebase version 8 below

firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) {
  // Send token to your backend via HTTPS
  // ...
}).catch(function(error) {
  // Handle error
});

Does anyone know how it is done in firebase version 9?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The getIdToken() needs to be imported from the Auth SDK as shown below:

import { getAuth, getIdToken } from "firebase/auth"

const auth = getAuth();
const user = auth.currentUser

if (user) {
  getIdToken(user).then((token) => {
    console.log(token)
  })
}

The same goes for getIdTokenResult() to get user’s custom claims.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading