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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With