firebase / firebase/firebase-admin-node

Authentication error when using `database` with emulators

Đang mở
#2,034 0 bình luận 3 reaction 1 người được giao Được @christhompsongoogle nhận Xem trên GitHub
api: database
Ngôn ngữ chính
TypeScript
Star
1.7k
Fork
419
Merge trung bình
3 ngày 10 giờ
Pull request đã merge (30 ngày)
16

Mô tả

### Describe your environment

* Operating System version: Docker / Linux / Debian 16
* Firebase SDK version: `firebase-admin@11.4.1`
* Firebase Product: database
* Node.js version: 16.17
* NPM version: 8.15

### Describe the problem

Using the `database` per `firebase-admin` with emulators throws an error.

#### Steps to reproduce:

```ts
import admin from 'firebase-admin'

process.env.GCLOUD_PROJECT = 'demo'
process.env.FIREBASE_AUTH_EMULATOR_HOST = 'localhost:9099'

const demoApp = admin.initializeApp({
databaseURL: 'http://localhost:9000/?ns=demo-default-rtdb',
})

// This uses the emulator and works.
await demoApp.auth().listUsers()
.then(l => console.log(l))

// This fails with FirebaseAppError.
// code: app/invalid-credential
// Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal. Error code: ENOTFOUND"
// in firebase-admin/lib/app/firebase-app.js:85:19
await demoApp.database().getRules()
.then(r => console.log(r))
```
### Working alternative:

Just supplying a stub for the `Credential` interface works, as the token is never verified.
I think this should be added to the docs at https://firebase.google.com/docs/emulator-suite/connect_rtdb#admin_sdks and https://firebase.google.com/docs/database/admin/start.

```ts
import admin from 'firebase-admin'

process.env.GCLOUD_PROJECT = 'demo'
process.env.FIREBASE_AUTH_EMULATOR_HOST = 'localhost:9099'

const demoApp = admin.initializeApp({
credential: {
getAccessToken: () => Promise.resolve({access_token: 'foo', expires_in: 3600}),
},
databaseURL: 'http://localhost:9000/?ns=demo-default-rtdb',
})

await demoApp.auth().listUsers()
.then(l => console.log(l))

await demoApp.database().getRules()
.then(r => console.log(r))
```

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.