Step 1: Add SDK Dependency

To get started, incorporate the OTPLESS SDK into your project. Update your app’s build.gradle file by adding the following dependency:

build.gradle
implementation 'io.github.otpless-tech:otpless-android-sdk:2.3.3'

Make sure to synchronize your Gradle project to fetch the dependency.

Step 2: Configure AndroidManifest.xml

Modify your AndroidManifest.xml to include an intent filter within the activity responsible for sign-up or sign-in. This setup is crucial for handling deep links.

AndroidManifest.xml
<intent-filter>
  <action android:name="android.intent.action.VIEW" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
  <data android:host="otpless" android:scheme="otpless.{{YOUR_APP_ID}}" />
</intent-filter>

Replace YOUR_APP_ID with your actual App ID provided in your OTPLESS dashboard.

Additionally, ensure your activity is set to singleTop launch mode and exported attribute is true:

AndroidManifest.xml
android:launchMode="singleTop"
android:exported="true"

Step 3: Configure Your Signup/Sign In Activity

Import SDK Classes

First, import necessary classes from the OTPLESS SDK:

import com.otpless.main.OtplessManager;
import com.otpless.main.OtplessView;

import com.otpless.dto.HeadlessRequest;
import com.otpless.dto.HeadlessResponse;
import com.otpless.dto.HeadlessChannelType;

Declare an OtplessView instance:

OtplessView otplessView;

Within your onCreate() method, initialize and set up the OTPLESS Sign in:

// Initialise OtplessView
otplessView = OtplessManager.getInstance().getOtplessView(this);
otplessView.initHeadless("YOUR_APP_ID", savedInstanceState);
otplessView.setHeadlessCallback(this::onHeadlessCallback);
otplessView.verifyIntent(getIntent());

Replace YOUR_APP_ID with your actual App ID provided in your OTPLESS dashboard.

Step 4: Handle Callback

Implement a callback method to handle the response from the OTPLESS SDK:

private void onHeadlessCallback(@NonNull final HeadlessResponse response) {
  if (response.getStatusCode() == 200) {
    JSONObject successResponse = response.getResponse();
  } else {
    String error = response.getResponse().optString("errorMessage");
  }
}

Override onNewIntent()

Ensure you override the onNewIntent() method to correctly handle intent verification:

if (otplessView != null) {
otplessView.verifyIntent(intent);
}

Step 5: Handle Back Press

Override the onBackPressed() method to manage back press actions properly:

// make sure you call this code before super.onBackPressed();
if (otplessView.onBackPressed()) return;

Step 6: Initiate Authentication

Initiate the authentication process based on the user’s selected method by using the initiate method of the SDK.

final HeadlessRequest request = new HeadlessRequest();
request.setPhoneNumber("YOUR_PHONE_NUMBER", "YOUR_COUNTRY_CODE");
otplessView.startHeadless(request, this::onHeadlessCallback);

(Optional): Verify OTP

To verify the OTP entered by the user, use the verify method with the necessary parameters.

final HeadlessRequest request = new HeadlessRequest();
request.setPhoneNumber("YOUR_PHONE_NUMBER", "YOUR_COUNTRY_CODE");
request.setOtp("XXXXXX");
 otplessView.startHeadless(request, this::onHeadlessCallback);

Object Attributes

AttributeMandatoryDescription
channelYesThe authentication method selected by the user.
phoneConditionalUser’s phone number (required if channel is PHONE).
countryCodeConditionalCountry code of the phone number (required if channel is PHONE).
emailConditionalUser’s email (required if channel is EMAIL).
channelTypeConditionalType of social login initiated (required if channel is OAUTH).