Dodgeball Client Trust SDK for JavaScript
Table of Contents
Purpose
Dodgeball enables developers to decouple security logic from their application code. This has several benefits including:
- The ability to toggle and compare security services like fraud engines, MFA, KYC, and bot prevention.
- Faster responses to new attacks. When threats evolve and new vulnerabilities are identified, your application's security logic can be updated without changing a single line of code.
- The ability to put in placeholders for future security improvements while focussing on product development.
- A way to visualize all application security logic in one place.
The Dodgeball Client Trust SDK for JavaScript makes integration with the Dodgeball API easy and is maintained by the Dodgeball team.
Prerequisites
You will need to obtain an API key for your application from the Dodgeball developer center.
Related
Check out the Dodgeball Trust Server SDK for how to integrate Dodgeball into your application's backend.
Installation
Use npm
to install the Dodgeball module:
npm install @dodgeball/trust-sdk-client
Alternatively, using yarn
:
yarn add @dodgeball/trust-sdk-client
Usage
React Applications
The Dodgeball Client SDK comes with a useDodgeball
hook that can be used in all of your components.
You'll first need to initialize the SDK with your public API key which can be found on the developer settings page. This only needs to be done once when the application first loads as in the example below:
import { useDodgeball } from "@dodgeball/trust-sdk-client";
import { useEffect, useSelector } from "react";
import { selectCurrentUser, selectCurrentSession } from "./selectors";
export default function MyApp() {
const dodgeball = useDodgeball('public-api-key...');
const currentSession = useSelector(selectCurrentSession);
const currentUser = useSelector(selectCurrentUser);
useEffect(() => {
dodgeball.track(currentSession?.id, currentUser?.id);
}, [currentSession?.id, currentUser?.id]);
return (
<div>
<h1>My App</h1>
<MyComponent/>
</div>
);
}
Below is a simple example of a component that performs a verification when an order is placed:
import { useDodgeball } from "@dodgeball/trust-sdk-client";
import { useState, useEffect } from "react";
import axios from "axios";
export default function MyComponent() {
const dodgeball = useDodgeball();
const [isPlacingOrder, setIsPlacingOrder] = useState(false);
const [isOrderPlaced, setIsOrderPlaced] = useState(false);
const [isOrderDenied, setIsOrderDenied] = useState(false);
const [error, setError] = useState(null);
useEffect(() => {
setIsPlacingOrder(false);
}, [isOrderPlaced, isOrderDenied])
const placeOrder = async (order, previousVerification = null) => {
const sourceToken = await dodgeball.getSourceToken();
const endpointResponse = await axios.post("/api/orders", { order }, {
headers: {
"x-dodgeball-source-token": sourceToken,
"x-dodgeball-verification-id": previousVerificationId
}
});
dodgeball.handleVerification(endpointResponse.data.verification, {
onVerified: async (verification) => {
await placeOrder(order, verification.id);
},
onApproved: async () => {
setIsOrderPlaced(true);
},
onDenied: async (verification) => {
setIsOrderDenied(true);
},
onError: async (error) => {
setError(error);
setIsPlacingOrder(false);
}
});
}
const onPlaceOrderClick = async () => {
setIsPlacingOrder(true);
const order = {}
await placeOrder(order);
}
return (
<div>
<h2>My Component</h2>
<p>
This component is using the Dodgeball Client SDK.
</p>
{isOrderPlaced ? (
<p>
Your order was placed!
</p>
) : (
<p>
{isOrderDenied && <span>Order was denied. Contact support.</span>}
<button onClick={onPlaceOrderClick} disabled={isPlacingOrder || isOrderDenied}>
{isPlacingOrder ? 'Placing Order...' : 'Place Order'}
</button>
{error && <div>{error}</div>}
</p>
)}
</div>
);
}
Non-React Applications
The Dodgeball Client SDK exports a Dodgeball
class that can be passed a public API key and an optional config object. See the constructor section for more information on configuration. You can find your public API key on the developer settings page.
You'll first need to initialize the SDK with your public API key which can be found on the developer settings page. This only needs to be done once when the SDK first loads as in the example below:
import { Dodgeball } from "@dodgeball/trust-sdk-client";
const dodgeball = new Dodgeball('public-api-key...');
const sourceToken = await dodgeball.getSourceToken();
When you know the ID of the currently logged-in user, call dodgeball.track()
.
const onSession = (currentSession) => {
dodgeball.track(currentSession?.id);
}
const onLogin = (currentSession, currentUser) => {
dodgeball.track(currentSession?.id, currentUser?.id);
}
Later, when you want to verify that a visitor is allowed to perform an action, call dodgeball.getSourceToken()
to get a token representing this device. Pass the returned sourceToken
to your API. Once your API returns a response, pass the verification
to dodgeball.handleVerification
along with a few callback functions:
const placeOrder = async (order, previousVerificationId = null) => {
const sourceToken = await dodgeball.getSourceToken();
const endpointResponse = await axios.post("/api/orders", { order }, {
headers: {
"x-dodgeball-source-token": sourceToken,
"x-dodgeball-verification-id": previousVerificationId
}
});
dodgeball.handleVerification(endpointResponse.data.verification, {
onVerified: async (verification) => {
await placeOrder(order, verification.id);
},
onApproved: async () => {
setIsOrderPlaced(true);
},
onDenied: async (verification) => {
setIsOrderDenied(true);
},
onError: async (error) => {
setError(error);
setIsPlacingOrder(false);
}
});
}
Loading via CDN
The Dodgeball Client SDK is also available via CDN at this url:
https://www.unpkg.com/@dodgeball/trust-sdk-client@latest/dist/umd/index.js
To load this in an HTML document:
<!doctype html>
<html>
<head>
<title>My Application</title>
<script type="text/javascript" async defer src="https://www.unpkg.com/@dodgeball/trust-sdk-client@latest/dist/umd/index.js" onload="onDodgeballLoaded()"></script>
<script>
async function onDodgeballLoaded() {
const dodgeball = new Dodgeball('public-api-key...');
const placeOrder = async (order, previousVerificationId = null) => {
const sourceToken = await dodgeball.getSourceToken();
const endpointResponse = await axios.post("/api/orders", { order }, {
headers: {
"x-dodgeball-source-token": sourceToken,
"x-dodgeball-verification-id": previousVerificationId
}
});
dodgeball.handleVerification(endpointResponse.data.verification, {
onVerified: async (verification) => {
await placeOrder(order, verification.id);
},
onApproved: async () => {
setIsOrderPlaced(true);
},
onDenied: async (verification) => {
setIsOrderDenied(true);
},
onError: async (error) => {
setError(error);
setIsPlacingOrder(false);
}
});
}
await placeOrder({
cart: [],
paymentMethod: {},
});
}
</script>
</head>
<body>
Your application's content...
</body>
</html>
API
Configuration
The package requires a public API key as the first argument to the constructor.
const dodgeball = new Dodgeball("public-api-key...");
Optionally, you can pass in several configuration options to the constructor:
const dodgeball = new Dodgeball("public-api-key...", {
apiVersion: "v1",
apiUrl: "https://api.dodgeballhq.com",
logLevel: "ERROR",
disableCookies: false,
isEnabled: true
});
Option | Default | Description |
---|
apiVersion | v1 | The Dodgeball API version to use. |
apiUrl | https://api.dodgeballhq.com | The base URL of the Dodgeball API. Useful for sending requests to different environments such as https://api.sandbox.dodgeballhq.com . |
logLevel | INFO | The level of logging to use. Possible options are TRACE , INFO , ERROR , or NONE . TRACE and INFO are useful for debugging. ERROR only logs errors. NONE turns off all logging. |
disableCookies | false | If true , the SDK will not use cookies to store the source token. |
isEnabled | true | If false , the SDK will not make external API calls. This is useful for local development. |
Track the User
The track
method is used to initialize any session and user information that is known. This method should be called as soon as you have a session ID and optionally a user ID. If you do not have a user ID, you can pass null
or omit the second argument.
dodgeball.track(sessionId, userId);
Argument | Type | Description |
---|
sessionId | string | The ID of the current session. This may be any unique string representing the user's activity within your system. If you don't have a session ID to use, pass the sourceToken returned from dodgeball.getSourceToken() . |
userId | string | The ID of the current user. |
Get a Source Token
The getSourceToken
method returns a token that represents a period of activity on the current device. This token should be passed to your API when you want to verify that a user is allowed to perform an action. Source tokens expire after 1 hour and are automatically refreshed by the SDK.
const sourceToken = await dodgeball.getSourceToken();
If async/await isn't your thing, you can invoke the getSourceToken
method with a callback function that will be called with the source token as an argument:
dodgeball.getSourceToken((sourceToken) => {});
Refresh a Source Token
The refreshSourceToken
method refreshes the current source token. This is useful if you want to refresh the token before it expires, as in the case of when a user logs out.
const newSourceToken = await dodgeball.refreshSourceToken();
Alternatively, you can invoke the refreshSourceToken
method with a callback function that will be called with the new source token as an argument:
dodgeball.refreshSourceToken((newSourceToken) => {});
Handle a Verification
The handleVerification
method is used to handle a verification response from your API. This method should be called after your API returns a response containing a verification object. The verification object contains information about the verification that was performed. The handleVerification
method takes a verification object and a config object containing several callback functions. The handleVerification
method will call the appropriate callback function based on the verification response.
dodgeball.handleVerification(
verification,
{
onVerified: async (verification) => {
},
onApproved: async () => {
},
onDenied: async (verification) => {
},
onPending: async (verification) => {
},
onBlocked: async (verification) => {
},
onError: async (verificationError) => {
}
},
{
maxDuration: 24 * 60 * 60 * 1000,
}
);
Argument | Type | Description |
---|
verification | object | The verification object returned from your API. |
context | object | An object containing several callback functions described above. |
config | object | An object containing several configuration options. |
config.maxDuration | number | The maximum amount of time to wait for a verification to complete. If the verification is still pending after this amount of time, the onError callback will be called with an error object. |