firebase / firebase/firebase-js-sdk

`auth/invalid-api-key` error will occur if it is not set, even when using the auth emulator

Open
#6,776 14 comments 10 reactions 0 assignees View on GitHub
api: auth bug needs-attention testing-sdk
Dominant language
TypeScript
Stars
5.1k
Forks
1k
Avg merge
2d 21h
Merged PRs (30d)
37

Description

### [REQUIRED] Describe your environment

* Operating System version: Windows 10
* Browser version: _____
* Firebase SDK version: 9.13.0
* Firebase Product: auth

### [REQUIRED] Describe the problem
same as #4882
related to [5218](https://github.com/firebase/firebase-tools/issues/5218)

When using the auth emulator, apikey probably should not be necessary, but an `auth/invalid-api-key` error will occur if it is not set.
```
FirebaseError: Firebase: Error (auth/invalid-api-key).

7 | });
8 | export const firestore = getFirestore(app);
> 9 | export const auth = getAuth(app);
| ^
10 |
11 | connectFirestoreEmulator(firestore, 'localhost', 8080);
12 | connectAuthEmulator(auth, 'http://localhost:8081');

at createErrorInternal (node_modules/.pnpm/@firebase+auth@0.20.11_@firebase+app@0.8.3/node_modules/@firebase/auth/src/core/util/assert.ts:142:44)
at _assert (node_modules/.pnpm/@firebase+auth@0.20.11_@firebase+app@0.8.3/node_modules/@firebase/auth/src/core/util/assert.ts:167:30)
at node_modules/.pnpm/@firebase+auth@0.20.11_@firebase+app@0.8.3/node_modules/@firebase/auth/src/core/auth/register.ts:68:11
at Component.instanceFactory (node_modules/.pnpm/@firebase+auth@0.20.11_@firebase+app@0.8.3/node_modules/@firebase/auth/src/core/auth/register.ts:95:10)
at Provider.Object..Provider.getOrInitializeService (node_modules/.pnpm/@firebase+component@0.5.21/node_modules/@firebase/component/src/provider.ts:318:33)
at Provider.Object..Provider.initialize (node_modules/.pnpm/@firebase+component@0.5.21/node_modules/@firebase/component/src/provider.ts:242:27)
at initializeAuth (node_modules/.pnpm/@firebase+auth@0.20.11_@firebase+app@0.8.3/node_modules/@firebase/auth/src/core/auth/initialize.ts:66:25)
at getAuth (node_modules/.pnpm/@firebase+auth@0.20.11_@firebase+app@0.8.3/node_modules/@firebase/auth/src/platform_node/index.ts:50:16)
at Object. (test/firebase.ts:9:28)
```

When I put a random value in `apiKey` it seems to work fine.
```typescript
export const app = initializeApp({
projectId: 'test',
apiKey: 'test'
});
```

The check for `apiKey` is done on the client side, which was appended before the auth emulator was created, so it is probably not needed.
https://github.com/firebase/firebase-js-sdk/blob/master/packages/auth/src/core/auth/register.ts#L68-L71

#### Steps to reproduce:

Initialize auth without specifying apiKey in initializeApp

#### Relevant Code:

```typescript
import { connectAuthEmulator, getAuth } from 'firebase/auth';
import { initializeApp } from 'firebase/app';

export const app = initializeApp({
projectId: 'test',
});
export const auth = getAuth(app); // <- error here

connectAuthEmulator(auth, 'http://localhost:8081');
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.