/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.
The server
Create amain.go file in that directory. It builds the two clients, holds them on a server value, and routes the two endpoints:
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.
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).
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:signInWithCustomToken or its equivalent in the Firebase SDK it uses.