Sample code for 30+ languages & platforms
React Native Requires Chilkat v11.0.0+

Duo Auth API - Preauth

See more Duo Auth MFA Examples

The /preauth endpoint determines whether a user is authorized to log in, and (if so) returns the user's available authentication factors.

Chilkat React Native Downloads

React Native
import { Http, HttpRequest, HttpResponse, JsonObject } from '@chilkat/react-native'

async function chilkatExample() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const integrationKey = 'DIMS3V5QDVG9J9ABRXC4';
  const secretKey = 'HWVQ46nubLBxhnRlKddTltWIi3hL0fIQF2qTvLab';

  const http = new Http();

  http.accept = 'application/json';

  // Use your own hostname here:
  const url = 'https://api-a03782e1.duosecurity.com/auth/v2/preauth';

  http.login = integrationKey;
  http.password = secretKey;

  const req = new HttpRequest();
  req.addParam('username', 'matt');

  req.httpVerb = 'POST';
  req.contentType = 'application/x-www-form-urlencoded';

  const resp = new HttpResponse();
  try {
    await http.httpReqAsync(url, req, resp);
  } catch {
    console.log(http.lastErrorText);
    return;
  }

  console.log(`status code = ${resp.statusCode}`);

  const json = new JsonObject();
  json.load(resp.bodyStr);
  json.emitCompact = false;
  console.log(json.emit());

  // Sample successful output:

  // status code = 200

  // {
  //   "response": {
  //     "devices": [
  //       {
  //         "capabilities": [
  //           "auto",
  //           "push",
  //           "sms",
  //           "mobile_otp"
  //         ],
  //         "device": "DP6GYVTQ5NK82BMR851F",
  //         "display_name": "iOS (XXX-XXX-1871)",
  //         "name": "",
  //         "number": "XXX-XXX-1871",
  //         "type": "phone"
  //       }
  //     ],
  //     "result": "auth",
  //     "status_msg": "Account is active"
  //   },
  //   "stat": "OK"
  // }

  // Sample code to parse the above JSON response..

  // Use this online tool to generate parsing code from sample JSON: 
  // Generate Parsing Code from JSON

  let device = '';
  let displayName = '';
  let name = '';
  let numberArg = '';
  let vType = '';
  let j = 0;
  let countJ = 0;
  let strVal = '';

  const responseResult = json.stringOf('response.result');
  const responseStatusMsg = json.stringOf('response.status_msg');
  const stat = json.stringOf('stat');
  let i = 0;
  const countI = json.sizeOfArray('response.devices');
  while (i < countI) {
    json.i = i;
    device = json.stringOf('response.devices[i].device');
    displayName = json.stringOf('response.devices[i].display_name');
    name = json.stringOf('response.devices[i].name');
    numberArg = json.stringOf('response.devices[i].number');
    vType = json.stringOf('response.devices[i].type');
    j = 0;
    countJ = json.sizeOfArray('response.devices[i].capabilities');
    while (j < countJ) {
      json.j = j;
      strVal = json.stringOf('response.devices[i].capabilities[j]');
      j++;
    }

    i++;
  }
}