mabg
mabg

Reputation: 2100

How to configure credentials file in a Go server application to use Firebase emulators

I have a server Go application on Google App Engine that uses Firebase Auth and Firestore.

func InitFirebase() {
    ctx := context.Background()
    opt := option.WithCredentialsFile("keys/firebase.json")
    app, err := firebase.NewApp(ctx, nil, opt)
    if err != nil {
        panic(err)
    }
    FirebaseAuth, err = app.Auth(ctx)
    if err != nil {
        panic(err)
    }
    Firestore, err = app.Firestore(ctx)
    if err != nil {
        panic(err)
    }
}

It has a json configuration file to access all the Firebase services. The firebase.json is downloaded from the Firebase console and contains all the parameters needed to connect the services:

{
  "type": "service_account",
  "project_id": "xxxx",
  "private_key_id": "xxxxx",
  "private_key": "-----BEGIN PRIVATE KEY----- xxxxx \n-----END PRIVATE KEY-----\n",
  "client_email": "xxxx.gserviceaccount.com",
  "client_id": "xxxx",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminxxxx.iam.gserviceaccount.com"
}

What values I must to put to connect to Firebase Firestore Emulator and maintain authentication?

Upvotes: 0

Views: 785

Answers (1)

mabg
mabg

Reputation: 2100

It doesn't need to change the json file.

Only defining the environment variable FIRESTORE_EMULATOR_HOST="localhost:8080", the connection is done automatically to the emulator.

If is using Visual Studio Code, define it in the launch.json file:

 "configurations": [
    {
        "name": "Launch",
        "type": "go",
        "request": "launch",
        "mode": "auto",
        "program": "${fileDirname}",
        "env": {
            "FIRESTORE_EMULATOR_HOST": "localhost:8080"
        },
        "args": []
    }
]

Upvotes: 2

Related Questions