Skip to main content
Here we show a sample Go application that verifies a user’s phone number with Prelude, then authenticates them in Firebase Auth, creating the account if it does not exist yet. The server exposes 2 endpoints. Clients call /send_code to send a verification code to a phone number, then /verify with the phone number and the code they received. If the code is correct, the response carries a Firebase custom token for the user. See the full example on GitHub.

Prerequisites

  • A recent Go version. To set this up, follow the Go docs.
  • A Prelude account. If you don’t have one already, you can sign up here.
  • A Firebase project with Auth enabled. Follow the Firebase docs.
Outside Google Cloud you also need a service account key file, generated from the Firebase project settings, in the “Service accounts” section.

Setting up the project

Create a new directory and configure the Go module and dependencies:
  • Create a new Go module: go mod init example.com/prelude-firebase-integration
  • Install the Prelude Go SDK: go get github.com/prelude-so/go-sdk
  • Install the Firebase SDK: go get firebase.google.com/go/v4
  • From the Prelude dashboard, select your application, go to Configure, Keys and click “Generate Key”. Make a note of this key as you can not reveal it again in the dashboard.
Both SDKs read their credentials from the environment, so the code holds no configuration of its own:

The server

Create a main.go file in that directory. It builds the two clients, holds them on a server value, and routes the two endpoints:
Neither client is given options: the Firebase SDK finds the service account through GOOGLE_APPLICATION_CREDENTIALS, and the Prelude SDK reads API_TOKEN.

Requesting a verification code

phone_number is an E.164 formatted mobile phone number. For instance, a French mobile number would be formatted as +33XXXXXXXXX.
We read the phone number from the body and pass it to Verification.New, which sends the code. On success the handler returns 204.

Checking the verification code

code is the verification code received on the phone (4 to 8 digits).
We pass the phone number and the code to Verification.Check. Only a Success status lets us mint a token; anything else means the code did not check out, and the client gets a 401.

Creating the Firebase user

At this point the phone number is verified, which is all Firebase needs to authenticate the user:
We look the user up by phone number, create them if there is none, and return a custom token for them. Your client then signs the user in with that token, through signInWithCustomToken or its equivalent in the Firebase SDK it uses.

Running it

Errors come back as a status code and a short message: 400 for a malformed body, 401 for a code that did not check out, 502 when the call to the Prelude API failed.

Conclusion

That is the whole integration. If you have questions about implementing Prelude, we’ll be happy to help at support@prelude.so.