New to node and firebase authentication so please be nice.
I was making my website and was using firebase authentication. so when i was making a call to one of my apis, i wanted to get the users id token from firebase and send it to the back end where i can validate the token.
so below is an example of my code snippet, but anytime i call someFunction, it keeps on returning the function itself but i want to get the result of what that function computes. am i taking the wrong approach?
export const someFunction = async () => {
return firebase.auth().onAuthStateChanged(async user => {
if (user) {
return user.getIdToken(true).then(async idToken => {
let resp = await axios({
headers: {
authorization: idToken
},
})
return {
isSuccess: resp.data.isSuccess,
message: resp.data.message
}
})
} else {
return {
isSuccess: false,
message: 'Could not validate user, please try again.'
}
}
})
}
Solution
thanks to the comments and then getting me onto the right path i found How to Promisify this function - nodejs which led me to solve it by:
export const someFunction = async () => {
// 1 - Create a new Promise
return new Promise(function (resolve, reject) {
// 2 - Copy-paste your code inside this function
firebase.auth().onAuthStateChanged(user => {
resolve(user)
})
})
}