'Firebase auth emulator was blocked

I got this error when I using local firebase emulator.

blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Private-Network' header was present in the preflight response for this private network request targeting the local address space.

Initializing firebase.

export const firebaseConfig = {
    apiKey: process.env.NEXT_PUBLIC_FIREBASE_APP_APIKEY,
    authDomain: process.env.NEXT_PUBLIC_FIREBASE_APP_AUTHDOMAIN,
    databaseURL: process.env.NEXT_PUBLIC_FIREBASE_APP_DATABASEURL,
    projectId: process.env.NEXT_PUBLIC_FIREBASE_APP_PROJECT_ID,
    storageBucket: process.env.NEXT_PUBLIC_FIREBASE_APP_STORAGE_BUCKET,
    messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_APP_MESSAGING_SENDER_ID,
    appId: process.env.NEXT_PUBLIC_FIREBASE_APP_APP_ID,
    measurementId: process.env.NEXT_PUBLIC_FIREBASE_APP_MEASUREMENT_ID
};

const firebase = initializeApp(firebaseConfig);

const store = initializeFirestore(firebase, {ignoreUndefinedProperties: true})
const db = getDatabase(firebase);
const auth = getAuth(firebase);
const storage = getStorage(firebase);

// @ts-ignore
if (process.env.NEXT_PUBLIC_MODE === 'emulator' && !auth.config.emulator) {
    connectDatabaseEmulator(db, "localhost", 9000);
    connectFirestoreEmulator(store, "localhost", 8080);
    connectAuthEmulator(auth, 'http://localhost:9099');
    connectStorageEmulator(storage, 'localhost', 9199);
}

export { store, db, auth, storage };
export default firebase;

I got error when I execute this code.

const userCredential = await setPersistence(auth, browserLocalPersistence).then(async () => {
      return await signInWithEmailAndPassword(auth, e.email, e.password).then((userCredential) => {
        return userCredential;
      }).catch((error) => {
        console.log(error);
        return null;
      });
    });

versions

node: v16.4.0
react: ^17.0.2
next: ^12.0.7
firebase: ^9.6.2

[email protected]
Chrome: 100.0.4896.75(Official Build) (x86_64)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source