Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the user ID token after sign-in on the web

I use firebase-admin for node.js and firebase for react. I want to get access token to send it to my server in header for defend my data. I use this code on react app

firebase.auth().signInWithEmailAndPassword(form.elements.email.value, form.elements.password.value)
                .then((result) => {
                        console.log(result)
                })
                .catch(function(error) {
                    var errorCode = error.code;
                    var errorMessage = error.message;
                    console.log(errorMessage);
                });

but I dont know how to get token from result. Or i have some ways to work with API on my server? It should work like this: if user want to get to admin panel, he should sign in. Client get token, set in cookie ans use with any request?

like image 207
Артём Котов Avatar asked Oct 25 '25 04:10

Артём Котов


1 Answers

The ID token is not available in the data returned from signInWithEmailAndPassword. What you can do instead is follow the documentation and use getIdToken() to get the token after sign-in finishes.

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

The linked documentation goes on to explain how to validate the token on the backend using the Firebase Admin SDK.

like image 162
Doug Stevenson Avatar answered Oct 26 '25 17:10

Doug Stevenson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!