Skip to main content
Version: Java

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:

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

Step 3: Initialize the application

gradle init --type java-application

Step 4: Add the Glide SDK Dependency

Open the build.gradle file and add the Glide SDK dependency inside the dependencies block:

dependencies {
// Other dependencies

// Glide SDK
implementation("com.glideapi:glide-sdk-java:1.0.2")
}
info

make sure to sync the project after adding the dependency or run the following command:

./gradlew build

Step 5: Perform a Verification

App.java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.glideapi.GlideClient;
import com.glideapi.services.MagicAuthClient;

public class App {
private static final String PHONE_NUMBER = "+555123456789";
public static void main(String[] args) {
try {
GlideClient glideClient = new GlideClient("<client_id>", "<client_secret>");
MagicAuthClient.MagicAuthStartResponse startResponse = glideClient.magicAuth.startAuth(
new MagicAuthClient.MagicAuthStartPropsPhone(PHONE_NUMBER),
null
);
// Check if magic auth type is supported
if ("MAGIC".equals(startResponse.type)) {
// Get verification token
HttpClient client = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.ALWAYS)
.build();
HttpResponse<String> response = client.send(
HttpRequest.newBuilder()
.uri(URI.create(startResponse.authUrl))
.GET()
.build(),
HttpResponse.BodyHandlers.ofString()
);
String token = response.headers().firstValue("token").orElseThrow(
() -> new RuntimeException("Failed to get verification token")
);
// Verify auth
MagicAuthClient.MagicAuthCheckResponse checkResponse = glideClient.magicAuth.verifyAuth(
MagicAuthClient.MagicAuthVerifyProps.createMagicProps(PHONE_NUMBER, token),
null
);
if (checkResponse != null) {
System.out.println("Magic auth verification result: " + checkResponse.verified);
}
} else {
// If not MAGIC type, verification code was sent to user's device
// through different channel (SMS/EMAIL/etc.) specified in startResponse.type
System.out.println("Verification code sent via: " + startResponse.type);
}
} catch (Exception e) {
System.err.println("Error during magic auth verification: " + e.getMessage());
}
}
}
NOTE

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.

Run the script from your IDE or you can use the following command:

./gradlew run

Conclusion

You have successfully performed your first verification using the Magical Auth API. You can now follow our 5-minute quickstart to deploying a test application using the Magical Auth API.