'Firebase Authentication Emulator not working: Requests with Authentication ignored

After long search I finally decided to ask here:

Whats working so far:

  • Emulator with Functions/Firestore and Authentication over Firebase Cloud
  • Mocha/Chai Unit Tests in Typescript
  • Debugging with Mocha Test Explorer in Visual Studio Code
  • Authentication/Functions and Firestore Emulator are activated in init

Now I want to get an Emulator working to debug authentication trigger functions

My code in my test.ts file to init firebase/emulator looks as following:

const options = {
apiKey: 'working-api',
appId: 'working-appID',
messagingSenderId: 'working-messagingSenderId',
projectId: 'my-project-id',
authDomain: 'my-project-id.firebaseapp.com',
storageBucket: 'my-project-id.appspot.com',
measurementId: 'working-measurementId',
} as FirebaseOptions;

initializeApp(options);
const auth = getAuth();
connectAuthEmulator(auth, "http://localhost:9099", {disableWarnings: false});

I installed firebase globally with commands:

npm install -g firebase npm install -g firebase-tools@latest

My package.json looks as following (which I install with npm install):

  "dependencies": {
    "@google-cloud/firestore": "^5.0.2",
    "@googlemaps/google-maps-services-js": "^3.3.7",
    "axios": "^0.25.0",
    "chai-as-promised": "^7.1.1",
    "crypto-js": "^4.1.1",
    "express": "^4.17.3",
    "firebase-admin": "^9.8.0",
    "firebase-functions": "^3.19.0",
    "lodash.isequal": "^4.5.0",
    "oas3-tools": "^2.2.3",
    "raw-body": "^2.4.3"
  },
  "devDependencies": {
    "@apidevtools/json-schema-ref-parser": "^9.0.9",
    "@types/chai-as-promised": "^7.1.5",
    "@types/google.maps": "^3.47.4",
    "@types/lodash.isequal": "^4.5.5",
    "@types/mocha": "^9.1.0",
    "@typescript-eslint/eslint-plugin": "^5.11.0",
    "@typescript-eslint/parser": "^5.11.0",
    "chai": "^4.3.6",
    "chai-http": "^4.3.0",
    "copyfiles": "^2.4.1",
    "eslint": "^7.6.0",
    "eslint-config-google": "^0.14.0",
    "eslint-plugin-import": "^2.25.4",
    "firebase-functions-test": "^0.2.0",
    "js-yaml": "^4.1.0",
    "mocha": "^9.2.0",
    "typescript": "^4.5.5"
  },

Normal authentication works with cloud as following:

      chai
      .request(host)
      .put(`/users/${id}`)
      .set({ Authorization: `Bearer ${id}` })
      .send(user)
      .then((res) => {                         
        res.should.have.status(200);
        expect(res.body).to.have.property("firstName");
        expect(res.body.id).to.equal(user.id, "id not equal" + id);
        resolve(true);
      })
      .catch((err) => {
        reject(err);
      })

Even if change the port in connectAuthEmulator nothing happens. Its completely ignored. Also Authentication is completely ignored and is used from the cloud, where its working.

I also tried deleting node_modules folder and reinstallating with npm and reinstalling firebase tools.

Did I forgot anything?

Thx for your help.



Sources

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

Source: Stack Overflow

Solution Source