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

Support new expiresAt/expiresIn/token/passed fields with trackOnce({ fraud: true }) and trackVerified() #159

Merged
merged 17 commits into from
May 6, 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.1.18/radar.min.js"></script>
<script src="https://js.radar.com/v4.2.0/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.1.18/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.1.18/radar.min.js"></script>
<link href="https://js.radar.com/v4.2.0/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.2.0/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.1.18/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.1.18/radar.min.js"></script>
<link href="https://js.radar.com/v4.2.0/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.2.0/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.1.18/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.1.18/radar.min.js"></script>
<link href="https://js.radar.com/v4.2.0/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.2.0/radar.min.js"></script>
</head>

<body>
Expand Down
1 change: 0 additions & 1 deletion demo/views/layouts/main.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
<ul class="btn-toggle-nav list-unstyled fw-normal pb-1 small">
<li><a href="./track-a-user" class="link-dark rounded">Track a user</a></li>
<li><a href="./track-verified" class="link-dark rounded">Track verified</a></li>
<li><a href="./track-verified-token" class="link-dark rounded">Track verified token</a></li>
<li><a href="./start-a-trip" class="link-dark rounded">Start a trip</a></li>
<li><a href="./log-a-conversion" class="link-dark rounded">Log a conversion</a></li>
<li class="border-top my-3"></li>
Expand Down
94 changes: 0 additions & 94 deletions demo/views/track-verified-token.hbs

This file was deleted.

32 changes: 25 additions & 7 deletions demo/views/track-verified.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@
<div class="mt-2">
<pre><code id="events" class="code-sample language-json"></code><pre>
</div>

<h6 class="mt-4">Token</h6>
<div class="mt-2">
<pre style="white-space: pre-wrap"><code id="token" class="code-sample language-json"></code><pre>
</div>

<h6 class="mt-2">Expires at</h6>
<div class="mt-2">
<pre><code id="expires-at" class="code-sample language-json"></code><pre>
</div>

<h6 class="mt-2">Passed</h6>
<div class="mt-2">
<pre><code id="passed" class="code-sample language-json"></code><pre>
</div>
</div>

<div class="mt-4">
Expand All @@ -59,10 +74,14 @@
Radar.initialize('&lt;RADAR_PUBLISHABLE_KEY&gt;');

Radar.trackVerified({ userId: 'test-user-id' })
.then(({ location, user, events }) =&gt; {
.then(({ location, user, events, token, expiresAt, expiresIn, passed }) =&gt; {
console.log(location);
console.log(user);
console.log(events);
console.log(token);
console.log(expiresAt);
console.log(expiresIn);
console.log(passed);
});
});
&lt;/script&gt;
Expand Down Expand Up @@ -92,14 +111,10 @@

const userId = $('#user-id').val();
Radar.trackVerified({ userId })
.then(({ location, user, events }) => {
console.log('TRACK RESPONSE', { location, user, events });
.then(({ location, user, events, token, expiresAt, expiresIn, passed }) => {
console.log('TRACK RESPONSE', { location, user, events, token, expiresAt, expiresIn, passed });
$('#loader').hide();

console.log(location);
console.log(user);
console.log(events);

if (!location) {
$('#map').hide();
$('#response-details').hide();
Expand Down Expand Up @@ -131,6 +146,9 @@
$('#location').html(JSON.stringify(location, null, 2));
$('#user').html(JSON.stringify(user, null, 2));
$('#events').html(JSON.stringify(events, null, 2));
$('#token').html(token);
$('#expires-at').html(expiresAt);
$('#passed').html(passed ? 'true' : 'false');
hljs.highlightAll();
})
.catch((err) => {
Expand Down
2 changes: 1 addition & 1 deletion 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.1.18",
"version": "4.2.0",
"description": "Web Javascript SDK for Radar, location infrastructure for mobile and web apps.",
"homepage": "https://radar.com",
"type": "module",
Expand Down
40 changes: 33 additions & 7 deletions src/api/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import TripsAPI from './trips';
import { signJWT } from '../util/jwt';
import { ping } from '../util/net';

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

class TrackAPI {
static async trackOnce(params: RadarTrackParams) {
Expand Down Expand Up @@ -160,15 +160,41 @@ class TrackAPI {
sclVal,
cslVal,
};

let { user, events, token, expiresAt } = response;
const location = { latitude, longitude, accuracy };
let passed;
let expiresIn;
if (expiresAt) {
expiresAt = new Date(expiresAt);
passed = user?.fraud?.passed && user?.country?.passed && user?.state?.passed;
expiresIn = (expiresAt.getTime() - Date.now()) / 1000;
Copy link
Contributor

Choose a reason for hiding this comment

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

should the name be expiresInSeconds to be explicit? No strong opinion here because jwt uses expiresIn as well and just has the docs saying it is in seconds

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't really include units in the field name anywhere else in our APIs, so prefer to keep it expiresIn (if I could go back in a time machine to 2017 I might have used this pattern, but too late now, rather optimize for consistency and brevity)

}

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

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

return trackRes;
}
} else {
response = await Http.request({
method: 'POST',
path: 'track',
data: body,
});
}

response = await Http.request({
method: 'POST',
path: 'track',
data: body,
});

const { user, events } = response;
const location = { latitude, longitude, accuracy };

Expand Down
27 changes: 13 additions & 14 deletions src/api/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Logger from '../logger';
import Session from '../session';
import Storage from '../storage';

import type { RadarTrackParams, RadarTrackResponse, RadarTrackTokenResponse } from '../types';
import type { RadarTrackParams, RadarTrackVerifiedResponse } from '../types';

class VerifyAPI {
static async trackVerified(params: RadarTrackParams, encrypted: Boolean = false) {
Expand Down Expand Up @@ -53,7 +53,7 @@ class VerifyAPI {
host: apple ? 'https://radar-verify.com:52516' : 'http://localhost:52516',
});

const { user, events, token } = response;
let { user, events, token, expiresAt } = response;
let location;
if (user && user.location && user.location.coordinates && user.locationAccuracy) {
location = {
Expand All @@ -62,24 +62,23 @@ class VerifyAPI {
accuracy: user.locationAccuracy,
};
}

if (encrypted) {
const trackTokenRes = {
token,
} as RadarTrackTokenResponse;

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

return trackTokenRes;
let passed;
let expiresIn;
if (expiresAt) {
expiresAt = new Date(expiresAt);
passed = user?.fraud?.passed && user?.country?.passed && user?.state?.passed;
expiresIn = (expiresAt.getTime() - Date.now()) / 1000;
}

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

if (options.debug) {
trackRes.response = response;
Expand Down
18 changes: 9 additions & 9 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ export class RadarLocationError extends RadarError {
}
}

export class RadarLocationPermissionsError extends RadarError {
export class RadarPermissionsError extends RadarError {
constructor(message: string) {
super(message);
this.name = 'RadarLocationPermissionsError';
this.name = 'RadarPermissionsError';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To match mobile SDKs

this.status = 'ERROR_PERMISSIONS';
}
}

export class RadarDesktopAppError extends RadarError {
export class RadarVerifyAppError extends RadarError {
constructor() {
super('Desktop app not running.');
this.name = 'RadarDesktopAppError';
this.status = 'ERROR_DESKTOP_APP';
super('Radar Verify app not running.');
this.name = 'RadarVerifyAppError';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To account for Radar Verify desktop AND mobile apps

this.status = 'ERROR_VERIFY_APP';
}
}

Expand Down Expand Up @@ -131,11 +131,11 @@ export class RadarServerError extends RadarError {
}
}

export class RadarTimeoutError extends RadarError {
export class RadarNetworkError extends RadarError {
constructor() {
super('Request timed out.');
this.name = 'RadarTimeoutError';
this.status = 'ERROR_TIMED_OUT';
this.name = 'RadarNetworkError';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To match mobile SDKs

this.status = 'ERROR_NETWORK';
}
}

Expand Down
Loading
Loading