DEV Community

Marouen Helali
Marouen Helali

Posted on

Exporting your Firebase Auth Users with Golang and Firebase-CLI

How to extract the emails of your Firebase app users in a format readable by most email delivery services such as MailChimp and MailGun.

https://webintegral.com.co/blog/como-ingresar-a-firebase-y-activar-el-plan-pago/firebase-gif/

TL;DR: https://github.com/Marwan01/import-firebase-users

Dependencies
Go
firebase-cli

Firebase CLI

Download and install firebase-cli: https://firebase.google.com/docs/cli

brew install firebase-cli
Enter fullscreen mode Exit fullscreen mode

Login to firebase-cli:

firebase login
Enter fullscreen mode Exit fullscreen mode

Get your project ID:

firebase projects:list
Enter fullscreen mode Exit fullscreen mode

Run the command to get your auth users data:

firebase auth:export save_file.csv --format=csv --project <PROJECT_ID>
Enter fullscreen mode Exit fullscreen mode

You should now have a csv save_file.csv that has all your users’ emails along with a bunch of scrambled extra data.

Golang Implementation

To get started, create a new Directory/Git Repository, and initialize and Go project within it:

mkdir import-firebase-users
cd import-firebase-users
touch splitclean.go
go mod init
Enter fullscreen mode Exit fullscreen mode

Let’s start by adding the skeleton code for our service:

package main
import (
 "fmt"
)
func main() {
 fmt.Println("Firebase email export start.")
}
Enter fullscreen mode Exit fullscreen mode

Let’s test our execution by running go run splitclean.go . We should expect to see in stdout: Firebase email export start .

Now we can start working on the main course of the script, which is getting the data csv from Firebase and cleaning it up. First let’s open and parse the csv into a reader that we can loop over and extract emails from:

 // Open the file
 csvfile, err := os.Open("save_file.csv")
 if err != nil {
  log.Fatalln("Couldn't open the csv file", err)
 }

 // Parse the file
 r := csv.NewReader(csvfile)
Enter fullscreen mode Exit fullscreen mode

Then let’s create an emails slice, and loop over the reader, and pick out the record that has the email:

 var emails []string
 // Iterate through the records
 for {
  // Read each record from csv
  record, err := r.Read()
  if err == io.EOF {
   break
  }
  if err != nil {
   log.Fatal(err)
  }
  emails = append(emails, record[1])
 }
Enter fullscreen mode Exit fullscreen mode

Now let’s create a .txt file and write a header column Email Address . This is required by most mail services, and makes it automatically convertible to .csv with a header, if that’s what you want to work with.

 // Create a .txt file
 f, err := os.Create("emails.txt")

 if err != nil {
  log.Fatal(err)
 }
 // write header for the file
 _, _ = f.WriteString("Email Address\n")
 // close file when done
 defer f.Close()
Enter fullscreen mode Exit fullscreen mode

Now for the last step, let’s loop over the email slice and add each email to its own line on the .txt file:

// loop over the emails slice
for i := 0; i < len(emails); i++ {
  _, err2 := f.WriteString(emails[i])
  _, _ = f.WriteString("\n")
  if err2 != nil {
   log.Fatal(err2)
  }
 }
Enter fullscreen mode Exit fullscreen mode

(Optional) Bash Script

We can simplify the execution one more step, by adding a two command bash script that will call the firebase-cli with a a Project ID parameter, and the your Golang script:

touch entrypoint.sh
Enter fullscreen mode Exit fullscreen mode

Add the two commands:

firebase auth:export save_file.csv --format=csv --project $1
go run splitclean.go
Enter fullscreen mode Exit fullscreen mode

Make your script executable:

chmod +x entrypoint.sh
Enter fullscreen mode Exit fullscreen mode

Finally run the script:

./entrypoint.sh <firebase project ID>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)