Verify a user using SMS PIN with the Java SDK

This guide shows how to verify a user in a Java application using the Verification API and Java SDK to make and then report an SMS PIN verification.

The following diagram demonstrates the flow that happens to verify a user:

verification-flow

  1. The end user visits your website or service and tries to log in.
  2. Your backend then makes a request to the Sinch platform and initiates an SMS PIN verification request.
  3. The Sinch platform sends an SMS message with a one time PIN code to the phone number of the user.
  4. The user enters the code they received.
  5. Your backend makes a report verification request using the code the user entered.
  6. If the code matches, your backend will receive a success result from Sinch.
  7. The user, now verified, can proceed to log in to your site or service.
In this guide you will learn:
Note:

Before you can get started, you need the following already set up:

Set up your Java application

To quickly get started setting up a simple client application using the Java SDK:

  1. If you haven't already, clone the sinch-sdk-java-quickstart repository.
  2. Navigate to the getting-started/verification/user-verification-using-sms-pin/client/src/main/resources folder.
  3. Open the config.properties file. Using the Verification app credentials from your Sinch Customer Dashboard, populate the following fields with your values:
FieldDescription
APPLICATION_API_KEYThe unique ID of your application.
APPLICATION_API_SECRETThe secret for your application.
  1. Save the file.

Package the application

Now that you've modified the application, you need to use the Maven CLI to create a package that you can then execute. Open a command prompt or terminal to the sinch-sdk-java-quickstart/getting-started/verification/user-verification-using-sms-pin/client folder and run the following command:
Copy
Copied
mvn package
This command creates the target folder and application.

Initiate your verification request

  1. Now you can execute the code and initiate your verification request. Run the following command:
    Copy
    Copied
    java -jar target/sinch-java-sdk-client-application-1.0-SNAPSHOT-jar-with-dependencies.jar
  2. Follow the prompts in the console and enter the phone number of the mobile handset to which you want to send the SMS PIN verification request.

    You should receive a text message to your mobile handset with a verification code. In a production scenario, this is the code that a user would enter into your app to verify their account.

    Troubleshooting tip

    If after running your app you receive an application credentials error response, you may have forgotten to save your file after adding your authentication values. This is an easy mistake to make! Try saving the file and running the app again.

    You should also see the JSON response in the console.

Report your SMS PIN code

Now that you've received the SMS PIN code to your mobile handset, it's time to report that code to the Sinch servers to complete the verification process.

  1. In the console, follow the prompts by entering the code you received on your mobile handset.
  2. If you entered the code that you received, you should see a success report response in your console.
  3. Enter Q in the console to quit the application or try sending a code to your mobile handset again and reporting an incorrect code to see what happens!

Additional resources

We'd love to hear from you!
Rate this content:
Still have a question?
 
Ask the community.