-
-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug: signInWithCredential() generates CORS error on "capacitor://localhost" on iOS #221
Comments
Seems like a duplicate of firebase/firebase-js-sdk#5020. |
Wow, that was a fast reply, thank you so much. I have seen that bug and it does indeed look very similar. But I'm on firebase@9.11.0 and editing
Thank you again and for your amazing work on this package. |
Okay, then please have a look at this issue: ionic-team/capacitor#5531 |
Thanks again! I have also looked at that bug, but it seems to be about AngularFire, while I'm using Ionic React. |
AngularFire is just a wrapper for the Firebase JS SDK. if (Capacitor.isNativePlatform()) {
return initializeAuth(getApp(), {
persistence: indexedDBLocalPersistence
})
} else {
return getAuth()
} |
Aha! Yes, this worked, and led me to firebase/firebase-js-sdk#5019 (comment) which I believe is my bug. Signing in & out is now working on both web & device, which means signed-in users are authenticated when they make database write/update requests to the Firestore Cloud DB. Hooray! Thank you so much. So my import { initializeApp, getApp } from "firebase/app"
import { Capacitor } from "@capacitor/core"
import { getAuth, initializeAuth, indexedDBLocalPersistence } from "firebase/auth"
const firebaseApp = initializeApp(config)
let auth
export function getFirebaseAuth() {
if (!auth) {
if (Capacitor.isNativePlatform()) {
auth = initializeAuth(getApp(), { persistence: indexedDBLocalPersistence })
} else {
auth = getAuth()
}
}
return auth
}
export default const firebaseApp For signing in, my import { FirebaseAuthentication } from '@capacitor-firebase/authentication'
import { GoogleAuthProvider, signInWithCredential } from 'firebase/auth'
import { getFirebaseAuth } from './firebase'
const signInWithGoogle = async () => {
const result = await FirebaseAuthentication.signInWithGoogle()
const credential = GoogleAuthProvider.credential(result.credential?.idToken)
const auth = getFirebaseAuth()
await signInWithCredential(auth, credential)
return result.user
} To get signing out to work, I needed to replace import { signOut } from 'firebase/auth'
import { getFirebaseAuth } from './firebase'
function signMeOut() {
const auth = getFirebaseAuth()
signOut(auth)
} And my import { getFirebaseAuth } from './firebase'
useEffect(() => {
getFirebaseAuth().onAuthStateChanged(user => {
if (!result) {
setUser(null)
} else {
setUser({ ...user })
}
})
async function getCurrentUser() {
const { user } = await FirebaseAuthentication.getCurrentUser()
if (user) {
setUser(user)
}
}
getCurrentUser()
}, [ setUser ]) |
Great, thanks for sharing. |
Plugin(s):
@capacitor-firebase/authentication
Platform(s):
iOS
Current behavior:
When following the steps documented here for signing into Firebase JavaScript SDK, calling
signInWithCredential
generates a CORS error sayingcapacitor://localhost is not allowed
.Expected behavior:
The call to
signInWithCredential
should succeed.Steps to reproduce:
Related code:
Other information:
I'm attempting to solve a problem where Firestore users are only authenticated when on the Ionic web platform, not on iOS, which effectively means read-only access to my Firestore database. My reading suggests that I need to make sure the user is signed in to both layers (native & web), but when I attempted that, I hit this CORs error.
Capacitor doctor:
The text was updated successfully, but these errors were encountered: