Your First Verification
This guide will help you get started with the Magical Auth API by going through the steps to perform your first 2FA verification.
Prerequisites
Before you begin, you need to have the following:
- subscribe to the Magical Auth service, (guide here)
Step 1: Get your API Credentials
To get your API Credentials, you can go to the Magical Auth Product Page and click on "Manage on Provider" to access the service dashboard. For a full guide on how to get your credentials, you can refer to the Registration guide.
Step 2: Create a new project
Create a new folder and enter the folder from a terminal.
mkdir magical-auth-test
cd magical-auth-test
go mod init magical-auth-test
Step 3: Initialize the SDK
go get github.com/GlideApis/sdk-go
Step 4: Perform a Verification
package main
import (
"net/http"
"github.com/GlideApis/sdk-go/pkg/glide"
"github.com/GlideApis/sdk-go/pkg/types"
"fmt"
)
func main() {
glideClient, err := glide.NewGlideClient(types.GlideSdkSettings{
ClientID: "<client_id>",
ClientSecret: "<client_secret>",
})
if err != nil {
// handle error
return
}
phoneNumber := "+555123456789"
magicAuthStartResponse, err := glideClient.MagicAuth.StartAuth(types.MagicAuthStartProps{
PhoneNumber: phoneNumber,
}, types.ApiConfig{})
if err != nil {
// handle error
return
}
// Check authentication type
if magicAuthStartResponse.Type == "MAGIC" {
// Get token from magic auth URL
tokenRes, err := http.Get(magicAuthStartResponse.AuthURL)
if err != nil {
// handle error
return
}
defer tokenRes.Body.Close()
token := tokenRes.Header.Get("token")
if token == "" {
// handle missing token error
return
}
magicAuthCheckResponse, err := glideClient.MagicAuth.VerifyAuth(types.MagicAuthVerifyProps{
PhoneNumber: phoneNumber,
Token: token,
}, types.ApiConfig{})
if err != nil {
// handle error
return
}
fmt.Println("Magic auth verification result:", magicAuthCheckResponse.Verified)
if magicAuthCheckResponse.Verified {
// Verification successful
} else {
// Verification failed
}
} else {
// If not MAGIC type, verification code was sent to user's device
// through different channel (SMS/EMAIL/etc.) specified in magicAuthStartResponse.Type
return
}
}
Since we are using the test number +555123456789
, the verification will always return type MAGIC
and provide an AuthUrl
. Unlike a regular MAGIC
verification where the link would need to be opened on the user's device, this test number will return a test URL that can be opened on any device.
Because of this we can simply fetch the URL and extract the token from the response headers directly from the script.
With a token received, you can then verify the user's phone number using the VerifyAuth
method.
Using the test number you should get back a response saying the verification was successful.
To run the script, you can use the following command:
go run main.go
Conclusion
You have successfully performed your first verification using the Magical Auth API.