Skip to main content
Version: Go

Magical Auth Reference

To perform a Magical Auth 2FA verification you need to the following steps:

  1. Initiate a 2FA verification session: This is done by sending a request from the user's device or from the backend by providing the user's phone number. The server will detect what verification channel the user is able to perform and initiate the verification process.
  2. Verify the user: This is done by sending the verification code to the server to check if the user has successfully verified their phone number.

The Magical Auth service is available through the MagicalAuthClient through the GlideClient.

MagicAuthClient Reference

Methods

1. StartServerAuth

Description: StartServerAuth starts the 2FA verification process by sending a request from the application server by providing the user's phone number or email.

Syntax:

StartServerAuth(props types.MagicAuthStartProps, conf types.ApiConfig) (*types.MagicAuthStartServerAuthResponse, error)

Parameters:

ParameterTypeDescription
propsMagicAuthStartPropsA struct containing the start auth properties.
confApiConfigA struct containing optional API configuration like session.

MagicAuthStartProps Properties:

PropertyTypeDescription
PhoneNumberstringOptional - The phone number to verify
EmailstringOptional - The email to verify
RedirectURLstringOptional - URL to redirect after verification
StatestringOptional - A state value that can be used to maintain state between the client and server
OTPConfirmationURLstringOptional - Custom URL for OTP confirmation page
RCSConfirmationURLstringOptional - Custom URL for RCS waiting page
FallbackChannelFallbackVerificationChannelOptional - Fallback verification method ('SMS', 'EMAIL', or 'NO_FALLBACK')

At least one of PhoneNumber or Email must be provided.

Returns:

  • *types.MagicAuthStartServerAuthResponse: A struct containing the session identifier and the auth URL to redirect the user to complete the verification.

MagicAuthStartServerAuthResponse Properties:

PropertyTypeDescription
SessionIDstringUnique session identifier for this verification attempt
AuthURLstringURL where the user should be directed to complete verification

Example:

import (
"github.com/GlideApis/sdk-go/pkg/glide"
"github.com/GlideApis/sdk-go/pkg/types"
)

func main() {
glideClient, err := glide.NewGlideClient(settings)
if err != nil {
panic(err)
}

// Start server auth with phone number
startResponse, err := glideClient.MagicAuth.StartServerAuth(types.MagicAuthStartProps{
PhoneNumber: "+555123456789",
}, types.ApiConfig{SessionIdentifier: "magic_auth_server_test"})

if err != nil {
panic(err)
}

fmt.Printf("Session ID: %s\n", startResponse.SessionID)
fmt.Printf("Auth URL: %s\n", startResponse.AuthURL)
}

2. CheckServerAuth

Description: CheckServerAuth checks the status of a server-side verification initiated with StartServerAuth.

Syntax:

CheckServerAuth(sessionID string, conf types.ApiConfig) (*types.MagicAuthCheckServerAuthResponse, error)

Parameters:

PropertyTypeDescription
sessionIDstringThe session ID received from StartServerAuth

Returns:

  • *types.MagicAuthCheckServerAuthResponse: A struct containing the verification status.

MagicAuthCheckServerAuthResponse Properties:

PropertyTypeDescription
StatusstringCurrent status of the verification ('PENDING' or 'COMPLETED')
VerifiedboolWhether the verification was successful (only meaningful when status is COMPLETED)

Example:

import (
"github.com/GlideApis/sdk-go/pkg/glide"
"github.com/GlideApis/sdk-go/pkg/types"
)

func main() {
glideClient, err := glide.NewGlideClient(settings)
if err != nil {
panic(err)
}

// Check server auth status
checkResponse, err := glideClient.MagicAuth.CheckServerAuth(
"session-id-from-start-server-auth",
types.ApiConfig{SessionIdentifier: "magic_auth_server_test"},
)

if err != nil {
panic(err)
}

if checkResponse.Status == "COMPLETED" {
if checkResponse.Verified {
fmt.Println("Verification successful!")
} else {
fmt.Println("Verification failed!")
}
} else {
fmt.Println("Verification still pending...")
}
}

3. StartAuth

Description: StartAuth starts the 2FA verification process by sending a request from the user's device or by providing the user's phone number.

Syntax:

StartAuth(props types.MagicAuthStartProps, conf types.ApiConfig) (*MagicAuthStartResponse, error)

Parameters:

PropertyTypeDescription
PhoneNumberstringOptional - The phone number to verify
EmailstringOptional - The email to verify
RedirectURLstringOptional - URL to redirect after verification
StatestringOptional - A state value that can be used to maintain state
FallbackChannelFallbackVerificationChannelOptional - Fallback verification method
DeviceIPAddressstringOptional - IP address of the end-user's device

One of PhoneNumber or Email must be provided.

DeviceIPAddress should be provided when the request is made from a server on behalf of the end-user device, ensuring accurate IP-based verification and geolocation services.

Returns:

  • *MagicAuthStartResponse: A struct containing the verification status.

MagicAuthStartResponse Properties:

PropertyTypeDescription
TypestringThe value can be MAGIC, SMS or EMAIL
AuthURLstringOptional - The URL to open on the user's device in case of MAGIC type
FlatAuthURLstringOptional - The plain URL to use if you don't want to redirect
OperatorIdstringOptional - The OperatorId

Example:

import (
"github.com/GlideApis/sdk-go/pkg/glide"
"github.com/GlideApis/sdk-go/pkg/types"
)

func main() {
glideClient, err := glide.NewGlideClient(settings)
if err != nil {
panic(err)
}

startResponse, err := glideClient.MagicAuth.StartAuth(types.MagicAuthStartProps{
PhoneNumber: "+555123456789",
}, types.ApiConfig{SessionIdentifier: "magic_auth_test"})

if err != nil {
panic(err)
}

if startResponse.Type == "MAGIC" {
fmt.Printf("Open this URL on the user's device: %s\n", startResponse.AuthURL)
} else {
fmt.Printf("Verification code sent to the user's device using channel %s\n", startResponse.Type)
}
}

4. VerifyAuth

Description: VerifyAuth checks the code/token received from the user's device to verify the user.

Syntax:

VerifyAuth(props types.MagicAuthVerifyProps, conf types.ApiConfig) (*MagicAuthVerifyRes, error)

Parameters:

PropertyTypeDescription
CodestringOptional - The code received from the user's device via EMAIL or SMS
PhoneNumberstringOptional - The phone number to verify if SMS or MAGIC
EmailstringOptional - The email to verify in case of EMAIL
TokenstringOptional - The token received from the user's device in case of MAGIC
DeviceIPAddressstringOptional - IP address of the end-user's device

Two parameters need to be sent based on the Type received from the StartAuth method:

  • MAGIC: Token and PhoneNumber
  • SMS: Code and PhoneNumber
  • EMAIL: Code and Email

Returns:

  • *MagicAuthVerifyRes: A struct containing the verification result.

MagicAuthVerifyRes Properties:

PropertyTypeDescription
VerifiedboolWhether the user is verified

Example:

import (
"github.com/GlideApis/sdk-go/pkg/glide"
"github.com/GlideApis/sdk-go/pkg/types"
)

func main() {
glideClient, err := glide.NewGlideClient(settings)
if err != nil {
panic(err)
}

verifyResponse, err := glideClient.MagicAuth.VerifyAuth(types.MagicAuthVerifyProps{
PhoneNumber: "+555123456789",
Token: "token-from-auth-response",
}, types.ApiConfig{SessionIdentifier: "magic_auth_test"})

if err != nil {
panic(err)
}

if verifyResponse.Verified {
fmt.Println("User verified")
} else {
fmt.Println("User not verified")
}
}

Error Handling

Each method in MagicAuthClient can return errors under certain conditions. Developers should handle these errors as part of their implementation. Common errors include:

  • Invalid input parameters
  • Network or service unavailability issues
  • Authentication errors
  • Invalid session errors

Type Definitions

ApiConfig

Configuration object for API requests.

Properties

PropertyTypeDescription
SessionIdentifierstringOptional - Identifier for the session
Session*SessionOptional - Session object for authentication

Session

Object representing a user session with an access token, expiration time, and associated scopes.

Properties

PropertyTypeDescription
AccessTokenstringThe access token for authentication
ExpiresAtint64Unix timestamp when the session expires
Scopes[]stringList of authorization scopes

FallbackVerificationChannel

Enum defining possible fallback verification methods:

const (
SMS FallbackVerificationChannel = "SMS"
EMAIL FallbackVerificationChannel = "EMAIL"
NO_FALLBACK FallbackVerificationChannel = "NO_FALLBACK"
)