GAミント至上主義

Web Monomaniacal Developer.

NestJS開発環境firebase-adminでAuthentication ローカルエミュレーターを複数利用する

https://firebase.google.com/docs/emulator-suite/connect_auth?hl=ja

Firebase Authentication エミュレータを開発環境で使用していたが、複数プロジェクトを一つのNestJSアプリで使う必要があったが、 ローカルエミュレーターの設定が環境変数FIREBASE_AUTH_EMULATOR_HOSTに依存しており、複数のプロジェクトを切り替えにハマったのでメモ。

結論としては、firebase-adminのauthを呼び出す直前にFIREBASE_AUTH_EMULATOR_HOSTを書き換える

firebase-adminのauthを呼び出す際にこのような関数を準備して共通して使うようにした。

実際はmodule化してあるけど簡略化すると以下のような感じ

import * as admin from 'firebase-admin'

getFirebaseAuth(emulatorHost?: string) {
    if (emulatorHost) {
      console.log('use firebase auth emulator', emulatorHost)
      process.env.FIREBASE_AUTH_EMULATOR_HOST = emulatorHost
    }
    return admin.auth()
  }