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 trackVerifiedToken() #119

Merged
merged 4 commits into from
Aug 23, 2023
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.3/radar.min.js"></script>
<script src="https://js.radar.com/v4.1.4/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.3/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.1.3/radar.min.js"></script>
<link href="https://js.radar.com/v4.1.4/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.1.4/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.3/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.1.3/radar.min.js"></script>
<link href="https://js.radar.com/v4.1.4/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.1.4/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.3/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.1.3/radar.min.js"></script>
<link href="https://js.radar.com/v4.1.4/radar.css" rel="stylesheet">
<script src="https://js.radar.com/v4.1.4/radar.min.js"></script>
</head>

<body>
Expand Down
1 change: 1 addition & 0 deletions demo/views/layouts/main.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<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: 94 additions & 0 deletions demo/views/track-verified-token.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<div class="content flex-grow-1 p-4">
<h1 class="mb-4">Track verified token</h1>
<div id="error-alert" class="alert alert-danger fade show" role="alert" style="display: none;">
<strong>Error:</strong> <span id="error-text">Something went wrong. Check the developer console for details.</span>
</div>

<p>Use the Radar Verify app to verify the user's current location. Returns a JSON Web Token (JWT). Verify the JWT server-side using your secret key.</p>

<div class="mt-5 mb-3 form-row">
<label for="user-id" class="col-form-label" style="width: 128px;">User ID</label>
<div>
<input type="text" class="form-control" id="user-id" value="test-user-id" style="width: 250px;">
</div>
</div>

<div>
<button id="track-verified" type="button" class="btn btn-primary" disabled>Call track verified token</button>
</div>

<div id="loader" class="spinner-border text-primary mt-4" role="status" style="display: none;">
</div>

<div id="response-details" style="display: none;">
<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>
</div>

<div class="mt-4">
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can probably omit the code samples on these. These should probably live in the docs (I only added them as a temporary solution until we go the UI kits in the docs).

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've actually found it helpful to be able to show the code sample when using this page in demos (which I'm doing a lot), but we can always remove later

<pre><code class="code-sample language-html">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8" /&gt;
&lt;meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" /&gt;
&lt;link href="https://js.radar.com/v{{sdk_version}}/radar.css" rel="stylesheet"&gt;
&lt;script src="https://js.radar.com/v{{sdk_version}}/radar.min.js"&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;script&gt;
$('#track-verified').click(() =&gt; {
Radar.initialize('&lt;RADAR_PUBLISHABLE_KEY&gt;');

Radar.trackVerifiedToken({ userId: 'test-user-id' })
.then(({ token }) =&gt; {
console.log(token);
});
});
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code>
</pre>
</div>
</div>

<script>
$('#publishableKey').on('change', () => {
const publishableKey = $('#publishableKey').val();
if (publishableKey) {
$('#track-verified').prop('disabled', false);
}
});

$('#track-verified').click(() => {
$('#error-alert').hide();
$('#response-details').hide();

const publishableKey = $('#publishableKey').val();
Radar.initialize(publishableKey, { debug: true });

$('#loader').show();

const userId = $('#user-id').val();
Radar.trackVerifiedToken({ userId })
.then(({ token }) => {
console.log('TRACK RESPONSE', { token });
$('#loader').hide();

console.log(token);

$('#response-details').show();
$('#token').html(token);
hljs.highlightAll();
})
.catch((err) => {
console.error(err);
$('#loader').hide();
$('#error-alert').show();
$('#error-text').html(err.message);
});
});

</script>
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.1.3",
"version": "4.1.4",
"description": "Web Javascript SDK for Radar, location infrastructure for mobile and web apps.",
"homepage": "https://radar.com",
"type": "module",
Expand Down
21 changes: 17 additions & 4 deletions src/api/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import Logger from '../logger';
import Session from '../session';
import Storage from '../storage';

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

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

// user indentification fields
Expand Down Expand Up @@ -40,6 +40,7 @@ class VerifyAPI {
sdkVersion: SDK_VERSION,
stopped: true,
userId,
encrypted,
};

const response: any = await Http.request({
Expand All @@ -49,16 +50,28 @@ class VerifyAPI {
host: 'https://radar-verify.com:52516',
});

const { user, events } = response;
const { user, events, token } = response;
let location;
if (user.location && user.location.coordinates && user.locationAccuracy) {
if (user && user.location && user.location.coordinates && user.locationAccuracy) {
location = {
latitude: user.location.coordinates[1],
longitude: user.location.coordinates[0],
accuracy: user.locationAccuracy,
};
}

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

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

return trackTokenRes;
}

const trackRes = {
user,
events,
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ class Radar {
return VerifyAPI.trackVerified(params);
}

public static trackVerifiedToken(params: RadarTrackParams = {}) {
return VerifyAPI.trackVerified(params, true);
}

public static getContext(params: Location) {
return ContextAPI.getContext(params);
}
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ export interface RadarTrackResponse extends RadarResponse {
events?: RadarEvent[];
}

export interface RadarTrackTokenResponse extends RadarResponse {
token?: String;
}

export interface RadarContextResponse extends RadarResponse {
location?: Location;
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.1.3';
export default '4.1.4';
Loading