0.0.9 • Published 7 months ago

@dtect/security-sdk-react v0.0.9

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

Security SDK React

Security API SDK for React by dtect.

<li>
  <a href="#getting-started">Getting Started</a>
  <ul>
    <li><a href="#installation">Installation</a></li>
  </ul>
</li>
<li>
  <a href="#implementation">Implementation</a>
</li>
<li><a href="#license">License</a></li>
<li><a href="#contact">Contact</a></li>

Getting Started

The dtect Security API helps you gather valuable insights about your visitors, enhancing the quality of your survey data. This library is responsible for collecting data about the visitor's browser and behavior, send everything to our API and return a Security Result or a Security Token.

Please refer to our official documentation to read full documentation on security api

If you want to use our API, please contact us

Installation

Install package

npm install @dtect/security-sdk-react

or

yarn add @dtect/security-sdk-react

Implementation

Read full documentation about implementation on our official docs

1. Wrap your application (or component) with <SecurityAPIProvider>.

  • Set your public client id as clientId
  • Set your public api key as apiKey

Read more about keys on our official docs

// index.ts or app.tsx
import React from "react";
import { SecurityAPIProvider } from "@dtect/security-sdk-react";
import App from "./App";
import Form from "./src/form";

function MyPage() {
  return (
    <SecurityAPIProvider
      clientId={"your-public-client-id-from-dashboard"} // required
      apiKey={"your-public-api-key-from-dashboard"} // required
    >
      <Form />
    </SecurityAPIProvider>
  );
}
PropTypeDescription
clientIdstringRequired. public client id
apiKeystringRequired. public api key

2. Use useSecurityApi hook inside your component to check for visitor data

useSecurityApi has getSecurityToken and getSecurityResult functions to validate visitor. We recommend you to get securityToken from getSecurityToken then consult securityResult.

// src/form.ts
import React from "react";
import { useSecurityApi } from "@dtect/security-sdk-react";
import App from "./App";

function Form() {
  const { getSecurityToken } = useSecurityApi();

  const handleSubmit = async () => {
    const response = await getSecurityToken({
      projectId: "your-project-id-for-deduplication", // required
      visitorId: "your-visitor-id",
      securitySettings: {
        countriesAllowed: ["usa", "can"],
      },
    });

    if (response?.securityToken) {
      // send securityToken to your backend server then consult dtect's securityResult API to get result with security
    }
  };

  return (
    <form>
      {/* ...some inputs */}
      <button type="submit" onClick={handleSubmit}>
        Submit
      </button>
    </form>
  );
}

export default Form;
PropTypeDescription
projectIdstringRequired. A unique identifier for your project, study or survey. It allows our system to group visitor data and identify duplicates
visitorIdstringId to identify the visitor that is being evaluated
metaDataobjectAny additional information you want to add to each request
securitySettingsobjectConfigures security settings. Includes countriesAllowed
countriesAllowedstring[]List of countries allowed for your project, survey, or study.

Errors

Read full documentation about errors on our official docs

Access the list of error enums from SecurityAPIError

SecurityAPIProvider

You will see these errors on console

EnumMessageDescription
MISSING_CREDENTIALSMissing Public Client ID or Public API KeyPlease provide valid Public Client ID and Public API Key when initializing our package.
INVALID_CREDENTIALSInvalid Public Client ID or Public API KeyEnsure you are using valid Public Client ID and Public API Key.
SDK_INIT_ERRORInternal Server Error during SDK InitializationAn unexpected error occurred on our servers during SDK initialization.
DUPLICATE_INITIALIZATIONMultiple SDK Initializations DetectedYou should initialize our package only once.
MAINTENANCE_MODEAPI Under MaintenanceThe API is currently undergoing maintenance and will be available again shortly.

getSecurityToken()/getSecurityResult()

ErrorMessageDescription
NOT_INITIALIZEDSDK Not InitializedThe SDK must be initialized before calling this function.
INTERNAL_SERVER_ERRORInternal Server Error during API RequestAn unexpected error occurred on our servers during the API request. Check server logs for details.
FAILED_TO_GET_SECURITY_TOKENFailed to Generate Security TokenAn error occurred while generating the Security Token.
FAILED_TO_GET_SECURITY_RESULTFailed to Retrieve Security ResultAn error occurred while retrieving the Security Result.
MAINTENANCE_MODEAPI Under MaintenanceThe API is currently undergoing maintenance and will be available again shortly.
// src/form.ts
import React from "react";
import { useSecurityApi, SecurityAPIError } from "@dtect/security-sdk-react";
import App from "./App";

function Form() {
  const { getSecurityToken } = useSecurityApi();

  const handleSubmit = async () => {
    try {
      const response = await getSecurityToken({
        projectId: "your-project-id-for-deduplication", // required
        visitorId: "your-visitor-id",
        securitySettings: {
          countriesAllowed: ["usa", "can"],
        },
      });

      if (response?.securityToken) {
        // send securityToken to your backend server then consult dtect's securityResult API to get result with security
      }
    } catch (error) {
      switch (error.message) {
        case SecurityAPIError.FAILED_TO_GET_SECURITY_TOKEN:
        // log internally
      }
    }
  };

  return (
    <form>
      {/* ...some inputs */}
      <button type="submit" onClick={handleSubmit}>
        Submit
      </button>
    </form>
  );
}

export default Form;

Contact

dtect - Contact us - support@dtect.io

0.0.9

7 months ago

0.0.8

8 months ago

0.0.7

11 months ago

0.0.6

11 months ago

0.0.5

11 months ago

0.0.4

11 months ago

0.0.3

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago