Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add skipVerifyApp param to trackVerified() and startTrackingVerified() #185

Merged
merged 9 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Radar.initialize('prj_test_pk_...', { /* options */ });

Add the following script in your `html` file
```html
<script src="https://js.radar.com/v4.4.4/radar.min.js"></script>
<script src="https://js.radar.com/v4.4.5/radar.min.js"></script>
```

Then initialize the Radar SDK
Expand All @@ -73,8 +73,8 @@ To create a map, first initialize the Radar SDK with your publishable key. Then
```html
<html>
<head>
<link href="https://js.radar.com/v4.4.4/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.4/radar.min.js"></script>
<link href="https://js.radar.com/v4.4.5/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.5/radar.min.js"></script>
</head>

<body>
Expand All @@ -98,8 +98,8 @@ To create an autocomplete input, first initialize the Radar SDK with your publis
```html
<html>
<head>
<link href="https://js.radar.com/v4.4.4/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.4/radar.min.js"></script>
<link href="https://js.radar.com/v4.4.5/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.5/radar.min.js"></script>
</head>

<body>
Expand Down Expand Up @@ -130,8 +130,8 @@ To power [geofencing](https://radar.com/documentation/geofencing/overview) exper
```html
<html>
<head>
<link href="https://js.radar.com/v4.4.4/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.4/radar.min.js"></script>
<link href="https://js.radar.com/v4.4.5/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.4.5/radar.min.js"></script>
</head>

<body>
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radar-sdk-js",
"version": "4.4.4",
"version": "4.4.5",
"description": "Web Javascript SDK for Radar, location infrastructure for mobile and web apps.",
"homepage": "https://radar.com",
"type": "module",
Expand Down
7 changes: 4 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type {
RadarSearchPlacesParams,
RadarStartTrackingVerifiedParams,
RadarTrackParams,
RadarTrackVerifiedParams,
RadarTrackVerifiedResponse,
RadarTripOptions,
RadarValidateAddressParams,
Expand Down Expand Up @@ -129,7 +130,7 @@ class Radar {
}
}

public static trackVerified(params: RadarTrackParams = {}) {
public static trackVerified(params: RadarTrackVerifiedParams = {}) {
return VerifyAPI.trackVerified(params);
}

Expand All @@ -141,8 +142,8 @@ class Radar {
return VerifyAPI.stopTrackingVerified();
}

public static getVerifiedLocationToken() {
return VerifyAPI.getVerifiedLocationToken();
public static getVerifiedLocationToken(params: RadarTrackVerifiedParams = {}) {
return VerifyAPI.getVerifiedLocationToken(params);
}

public static setExpectedJurisdiction(countryCode?: string, stateCode?: string) {
Expand Down
2 changes: 2 additions & 0 deletions src/api/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class TrackAPI {

let sclVal = -1;
let cslVal = -1;
/*
try {
const [sclRes, csl] = await Promise.all([
Http.request({
Expand All @@ -126,6 +127,7 @@ class TrackAPI {
} catch (err) {
// do nothing, send scl = -1 and csl = -1
}
*/

const payload = {
payload: JSON.stringify({
Expand Down
133 changes: 73 additions & 60 deletions src/api/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import Http from '../http';
import Logger from '../logger';
import Session from '../session';
import Storage from '../storage';
import TrackAPI from './track';

import type { RadarStartTrackingVerifiedParams, RadarTrackParams, RadarTrackVerifiedResponse } from '../types';
import type { RadarStartTrackingVerifiedParams, RadarTrackVerifiedParams, RadarTrackVerifiedResponse } from '../types';

let tokenTimeoutId: any | null = null;
let tokenCallback: ((token: RadarTrackVerifiedResponse) => void) | null = null;
Expand All @@ -16,13 +17,15 @@ let expectedCountryCode: string | null = null;
let expectedStateCode: string | null = null;

class VerifyAPI {
static async trackVerified(params: RadarTrackParams, encrypted: Boolean = false) {
static async trackVerified(params: RadarTrackVerifiedParams, encrypted: Boolean = false) {
const options = Config.get();

const { skipVerifyApp } = params;

// user indentification fields
const userId = params.userId || Storage.getItem(Storage.USER_ID);
const deviceId = params.deviceId || Device.getDeviceId();
const installId = params.installId || Device.getInstallId();
Comment on lines -24 to -25
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the change to get rid of setting these via params? This might be how we override the values int the simulator.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really want to give developers control of these for fraud / geo-compliance use cases. For implementations using the Mac and Windows app, those apps override these anyway (with more stable hardware device IDs)

const deviceId = Device.getDeviceId();
const installId = Device.getInstallId();
const sessionId = Session.getSessionId();
const description = params.description || Storage.getItem(Storage.DESCRIPTION);

Expand All @@ -36,59 +39,69 @@ class VerifyAPI {
// other info
const metadata = params.metadata || Storage.getJSON(Storage.METADATA);

const body = {
...params,
description,
deviceId,
foreground: true,
installId,
sessionId,
metadata,
sdkVersion: SDK_VERSION,
stopped: true,
userId,
encrypted,
expectedCountryCode,
expectedStateCode,
};

let userAgent = navigator.userAgent;
const apple = userAgent && (userAgent.toLowerCase().includes('mac') || userAgent.toLowerCase().includes('iphone') || userAgent.toLowerCase().includes('ipod') || userAgent.toLowerCase().includes('ipad'));

const response: any = await Http.request({
method: 'GET',
path: 'verify',
data: body,
host: apple ? 'https://radar-verify.com:52516' : 'http://localhost:52516',
});

let { user, events, token, expiresAt, expiresIn, passed, failureReasons, _id } = response;
let location;
if (user && user.location && user.location.coordinates && user.locationAccuracy) {
location = {
latitude: user.location.coordinates[1],
longitude: user.location.coordinates[0],
accuracy: user.locationAccuracy,
let trackRes: RadarTrackVerifiedResponse;
if (skipVerifyApp) {
trackRes = await TrackAPI.trackOnce({
userId: userId ?? undefined,
description: description ?? undefined,
metadata: metadata,
fraud: true,
});
} else {
const body = {
...params,
description,
deviceId,
foreground: true,
installId,
sessionId,
metadata,
sdkVersion: SDK_VERSION,
stopped: true,
userId,
encrypted,
expectedCountryCode,
expectedStateCode,
};
}
if (expiresAt) {
expiresAt = new Date(expiresAt);
}

const trackRes = {
user,
events,
location,
token,
expiresAt,
expiresIn,
passed,
failureReasons,
_id,
} as RadarTrackVerifiedResponse;

if (options.debug) {
trackRes.response = response;

let userAgent = navigator.userAgent;
const apple = userAgent && (userAgent.toLowerCase().includes('mac') || userAgent.toLowerCase().includes('iphone') || userAgent.toLowerCase().includes('ipod') || userAgent.toLowerCase().includes('ipad'));

const response: any = await Http.request({
method: 'GET',
path: 'verify',
data: body,
host: apple ? 'https://radar-verify.com:52516' : 'http://localhost:52516',
});

let { user, events, token, expiresAt, expiresIn, passed, failureReasons, _id } = response;
let location;
if (user && user.location && user.location.coordinates && user.locationAccuracy) {
location = {
latitude: user.location.coordinates[1],
longitude: user.location.coordinates[0],
accuracy: user.locationAccuracy,
};
}
if (expiresAt) {
expiresAt = new Date(expiresAt);
}

trackRes = {
user,
events,
location,
token,
expiresAt,
expiresIn,
passed,
failureReasons,
_id,
} as RadarTrackVerifiedResponse;

if (options.debug) {
trackRes.response = response;
}
}

lastToken = trackRes;
Expand All @@ -103,7 +116,7 @@ class VerifyAPI {

static async startTrackingVerified(params: RadarStartTrackingVerifiedParams) {
const doTrackVerified = async () => {
const trackRes = await this.trackVerified({});
const trackRes = await this.trackVerified(params);

const { interval } = params;

Expand Down Expand Up @@ -143,16 +156,16 @@ class VerifyAPI {
}
}

static async getVerifiedLocationToken() {
static async getVerifiedLocationToken(params: RadarTrackVerifiedParams) {
const lastTokenElapsed = (performance.now() - lastTokenNow) / 1000;

if (lastToken) {
if (lastToken && lastToken.passed) {
if (lastTokenElapsed < (lastToken.expiresIn || 0)) {
return lastToken;
}
}

return this.trackVerified({});
return this.trackVerified(params);
}

static setExpectedJurisdiction(countryCode?: string, stateCode?: string) {
Expand Down
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,16 @@ export interface RadarTrackParams {
fraud?: boolean;
}

export interface RadarTrackVerifiedParams {
userId?: string;
description?: string;
metadata?: RadarMetadata;
skipVerifyApp?: boolean;
}

export interface RadarStartTrackingVerifiedParams {
interval: number;
skipVerifyApp?: boolean;
}

export enum RadarEventConfidence {
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default '4.4.4';
export default '4.4.5';
Loading