i'm abstracting my firebase api calls out of my react native components to a service layer. This is working well for calls that return a promise, but this one call, onAuthStateChanged, doesn't return a promise. Without the service layer i would just do:
firebase.auth().onAuthStateChanged(user => {
if (user) {
//logged in
} else { //not logged in }
Now i want to put everything in my services/api.js, i tried several things already but the latest was this:
export const userLoggedin = () => {
firebase.auth().onAuthStateChanged(user => {
if (user) {
return true
} else {
return false
}
})
}
Then in my app.js i want to check if userLoggedin returns true or false, so i can navigate the user depending if he is already logged in.
if (userLoggedin()) {
// logged in
} else {
// logged out
}
now this last part is always going to be in the else, because the userLoggedin return true way later, and it doesn't wait for it. What is a good solution for this problem?