/send_code
and /verify
.
Clients of this service will use the /send_code
endpoint to request the verification of a phone number. The phone number referenced here will receive a SMS message with the verification code. Then a second call to /verify
including the phone number and the received verification code will validate that the code is correct and, if so, a user is created or authenticated (if they already exist) in Firebase Auth.
Prerequisites
This sample uses the current Prelude Go SDK (v0.1.0) compatible with the Prelude V2 API, and the current Firebase Go SDK (v4). To run it correctly you will need:- 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 account and a project set up. Follow the instructions here. You will need Auth to be enabled and we will use the Firebase Admin SDK, as this is a server application. From the Firebase project settings (in the “Service accounts” section) you will need to generate a new private key file if you don’t already have one.
Implementing the example application
Setting up the project
Create a new directory somewhere in your system 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@latest
- Have the Firebase Service account file available as we will need it in the code.
- In your Prelude account dashboard, select the application you want to use and generate an API key if you don’t have it already.
- Select your application, go to Settings, Keys and click “Create”. Make a note of this key as you can not reveal it again in the dashboard and will need to create a new one if you lose it.
Some global configuration
The sample aims to be as simple as possible so we are going to have a few top level constants to hold our configuration parameters, namely the port we want our HTTP server to listen to, the path to our Firebase service account file, and the Prelude SDK key we got from the Prelude application settings. We can create aserver.go
file in the same directory where we created the Go module, use it as our main entry point, and add the required imports and constants at the top of the file:
The main function
We want to theserver.go
file to contain everything in this example so we are adding a main
function in it that will configure the HTTP server and the 2 required endpoints.
- Configured the required Firebase clients (the Firebase app and the Firebase Auth client) using the Firebase service account file. This used the installed Firebase Go SDK.
- Configured the Prelude client with the Prelude API Key. This used the Prelude Go SDK.
- Defined the 2 required endpoints (
/send_code
and/verify
) which will subsequently call the functions described below. - Start a simple HTTP server on the configured port.
Requesting a verification code
In this example, to initiate the verification of a phone number, we need to interact with the/send_code
endpoint. To achieve this, a client application should issue a POST HTTP request with the following structure:
phone_number
represents a E.164 formatted mobile phone number. For instance, a French mobile number would be formatted as: +33XXXXXXXXX
.
Upon receiving such a request, the server will invoke the SendCode
function, which will be outlined next:
preludeClient.Verification.New
function with it. If the Prelude client is correctly configured with the API key, a verification code will be sent to the requested phone number. In the success scenario (happy path) this function just returns a HTTP 204 status code.
Checking the verification code
Once the mobile phone receives the verification code, the client application must call the/verify
endpoint of the server to verify that the code is correct. The expected request for this endpoint has the following shape:
phone_number
field plus a new code
field that should be filled with the verification code received in the phone (from 4 to 8 digits code).
Once the endpoint receives the request, it will call the Check
function:
preludeClient.Verification.Check
function from the Prelude Go SDK with those parameters. Here we can simply examine the returned result and if it is a successful result (prelude.VerificationCheckResponseStatusSuccess
) we can proceed to create or authenticate the user in Firebase. For that we will use the CreateFirebaseUser
function.