I'm developing an app with a Sign In with Google Firebase. I've got the need to update de current user data wich exists within a Auth Module when a user sign in, and then chain the execution of stuff with that specific data. Something like this :
fbAuth.signInWithGoogle()
.then(()=>{
let user = fbAuth.getCurrentUser()
console.log(user);
fbFirestore.postDocToExistingGroup("projects", user.uid, {user: user.uid, test:true})
})
So I coded the function as a Promise inside a module Pattern function named fbAuth
, but it seems to not be the best way for me:
const signInWithGoogle = function(){
return Promise.resolve(
signInWithPopup(auth, provider)
.then((result) => {
// This gives you a Google Access Token. You can use it to access the Google API.
const credential = GoogleAuthProvider.credentialFromResult(result);
const token = credential.accessToken;
// The signed-in user info.
const user = result.user;
setCurrentUser(user)
console.log("Signed In", user)
// ...
}).catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const email = error.email;
// The AuthCredential type that was used.
const credential = GoogleAuthProvider.credentialFromError(error);
// ...
})
)
}
I did that just to be sure that I've got the current user data to execute the rest of the code using that data and not because the function its suposed to be a Promise from the beginning.