Issue
I've been struggling with this one for a while now and I've finally given up finding an answer to it, decided to ask my own question.
I have an Ionic 7 Vue app where I use Firebase Auth for user management. My code works perfectly in browsers and on Android and I can successfully sign users in. However on an iOS device or simulator it does not work.
When a user signs in I fire the following function (note that the auth object is valid, just not showing it here):
import {signInWithEmailAndPassword, UserCredential } from "firebase/auth";
const signInUser = async (email: string, password: string) => {
try{
const res = await signInWithEmailAndPassword(auth, email, password)
console.log("Successfully logged in with Firebase, UID: "+res.user.uid)
}
catch(error){
const errorCode = error.code;
const errorMessage = error.message;
console.error("Error Code: " + errorCode + " - Error Message: " + errorMessage);
}
}
The error condition fires successfully, with the console.log showing the correct error. For example, if I input an incorrect password it console.logs a message saying such.
However, if I input the correct password and email, the console.log within the try block never executes. It simply hangs there and does nothing, as if the promise from the await never resolves. I also can't see any errors being thrown in my Xcode output.
An interesting note is that within the Firebase console it shows that the login actually does happen. This all indicates to me that it actually is working and somehow I'm not handling the resolved promise properly somehow.
I'm hoping that I've simply been silly with something trivial and someone here is going to point out some typo or something. Or that someone knows of some workaround to this problem.
Solution
After contacting Firebase support and doing many hours of searching I finally found the issue. Answering here in case someone else has this problem and is so completely driven insane by it as I was.
Apparently native environments don't gel with the getAuth
function very well and can cause such issues. Setting up your auth
object therefore requires you use initializeAuth
instead. Before the change my code looked like this:
auth = getAuth(app);
setPersistence(auth,indexedDBLocalPersistence)
And to fix my problem I did this:
let auth: Auth;
if(isPlatform('hybrid')){
auth = initializeAuth(app, {persistence: indexedDBLocalPersistence})
}else{
auth = getAuth(app);
setPersistence(auth,indexedDBLocalPersistence)
}
Just remember to import all the needed items as they will not work otherwise:
import { initializeAuth, getAuth, setPersistence, indexedDBLocalPersistence, Auth } from "firebase/auth";
import { isPlatform } from "@ionic/vue";
Using this method I was able to get my iOS device and simulator to log in AND get a resolved, successful promise back from signInWithEmailAndPassword()
Answered By - Toiletduck
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.