diff --git a/README.md b/README.md index 6eb14a71..ef162016 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Radar.initialize('prj_test_pk_...', { /* options */ }); Add the following script in your `html` file ```html - + ``` Then initialize the Radar SDK @@ -73,8 +73,8 @@ To create a map, first initialize the Radar SDK with your publishable key. Then ```html - - + + @@ -98,8 +98,8 @@ To create an autocomplete input, first initialize the Radar SDK with your publis ```html - - + + @@ -130,8 +130,8 @@ To power [geofencing](https://radar.com/documentation/geofencing/overview) exper ```html - - + + diff --git a/package-lock.json b/package-lock.json index 92f02483..339ebda1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "radar-sdk-js", - "version": "4.4.8", + "version": "4.4.10-beta.1", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/package.json b/package.json index 9f0b3fd3..d4396a29 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "radar-sdk-js", - "version": "4.4.8", + "version": "4.4.10-beta.1", "description": "Web Javascript SDK for Radar, location infrastructure for mobile and web apps.", "homepage": "https://radar.com", "type": "module", diff --git a/src/api/track.ts b/src/api/track.ts index 3862a44a..5add1e7e 100644 --- a/src/api/track.ts +++ b/src/api/track.ts @@ -89,7 +89,6 @@ class TrackAPI { let response: any; if (fraud) { const host = 'https://api-verified.radar.io'; - const pingHost = 'ping.radar-verify.com'; const lang = navigator.language; const langs = navigator.languages; @@ -109,31 +108,9 @@ class TrackAPI { }, }); - let sclVal = -1; - let cslVal = -1; - /* - try { - const [sclRes, csl] = await Promise.all([ - Http.request({ - host: `https://${pingHost}`, - method: 'GET', - path: 'ping', - }), - ping(`wss://${pingHost}`), - ]); - const { scl }: any = sclRes; - sclVal = scl; - cslVal = csl; - } catch (err) { - // do nothing, send scl = -1 and csl = -1 - } - */ - const payload = { payload: JSON.stringify({ ...body, - scl: sclVal, - csl: cslVal, lang, langs, }), @@ -153,17 +130,6 @@ class TrackAPI { }, }); - if (options.debug && response && response.user) { - if (!response.user.metadata) { - response.user.metadata = {}; - } - - response.user.metadata['radar:debug'] = { - sclVal, - cslVal, - }; - } - let { user, events, token, expiresAt, expiresIn, passed, failureReasons, _id } = response; const location = { latitude, longitude, accuracy }; if (expiresAt) { diff --git a/src/api/verify.ts b/src/api/verify.ts index 9bcb6107..43b94748 100644 --- a/src/api/verify.ts +++ b/src/api/verify.ts @@ -15,8 +15,27 @@ let lastToken: RadarTrackVerifiedResponse | null = null; let lastTokenNow: number = 0; let expectedCountryCode: string | null = null; let expectedStateCode: string | null = null; +let lastIp: string | null = null; class VerifyAPI { + static async checkIpChanges() { + const { ip }: any = await Http.request({ + method: 'GET', + path: 'ping', + }); + + const ipChanged = ip !== lastIp; + if (ipChanged) { + Logger.info(`IP changed from ${lastIp} to ${ip}`); + + lastToken = null; + } + + lastIp = ip; + + return ipChanged; + } + static async trackVerified(params: RadarTrackVerifiedParams, encrypted: Boolean = false) { const options = Config.get(); @@ -104,6 +123,11 @@ class VerifyAPI { } } + if (params.ipChanges && trackRes?.user?.ip) { + lastIp = trackRes?.user?.ip; + Logger.info(`Setting ip to ${lastIp}`); + } + lastToken = trackRes; lastTokenNow = performance.now(); @@ -115,6 +139,12 @@ class VerifyAPI { } static async startTrackingVerified(params: RadarStartTrackingVerifiedParams) { + let { interval } = params; + if (!interval) { + interval = 60; + Logger.info(`interval not provided, using 60 seconds`); + } + const doTrackVerified = async () => { let trackRes; try { @@ -122,8 +152,6 @@ class VerifyAPI { } catch (err: any) { Logger.error(`trackVerified error: ${err.message}`); } - - const { interval } = params; let expiresIn = 0; let minInterval = interval; @@ -164,6 +192,10 @@ class VerifyAPI { static async getVerifiedLocationToken(params: RadarTrackVerifiedParams) { const lastTokenElapsed = (performance.now() - lastTokenNow) / 1000; + if (params.ipChanges) { + await this.checkIpChanges(); + } + if (lastToken && lastToken.passed) { if (lastTokenElapsed < (lastToken.expiresIn || 0)) { return lastToken; diff --git a/src/http.ts b/src/http.ts index 625276e6..60e06a11 100644 --- a/src/http.ts +++ b/src/http.ts @@ -189,12 +189,16 @@ class Http { if (host && (host === 'http://localhost:52516' || host === 'https://radar-verify.com:52516')) { reject(new RadarVerifyAppError()); } else { - reject(new RadarServerError()); + reject(new RadarNetworkError()); } } xhr.ontimeout = function () { - reject(new RadarVerifyAppError()); + if (host && (host === 'http://localhost:52516' || host === 'https://radar-verify.com:52516')) { + reject(new RadarVerifyAppError()); + } else { + reject(new RadarNetworkError()); + } } xhr.send(JSON.stringify(body)); diff --git a/src/types.ts b/src/types.ts index c10c82a8..32f152ae 100644 --- a/src/types.ts +++ b/src/types.ts @@ -81,11 +81,13 @@ export interface RadarTrackVerifiedParams { description?: string; metadata?: RadarMetadata; skipVerifyApp?: boolean; + ipChanges?: boolean; } export interface RadarStartTrackingVerifiedParams { interval: number; skipVerifyApp?: boolean; + ipChanges?: boolean; } export enum RadarEventConfidence { @@ -217,6 +219,7 @@ export interface RadarUser { dma?: RadarRegion; postalCode?: RadarRegion; fraud?: RadarFraud; + ip?: string; } export interface RadarTrackResponse extends RadarResponse { diff --git a/src/ui/RadarMap.ts b/src/ui/RadarMap.ts index 7d04429a..09593531 100644 --- a/src/ui/RadarMap.ts +++ b/src/ui/RadarMap.ts @@ -96,7 +96,7 @@ class RadarMap extends maplibregl.Map { radarMapOptions, { style }, ); - Logger.debug('map initailized with options', mapOptions); + Logger.debug('map initialized with options', mapOptions); (mapOptions as maplibregl.MapOptions).transformRequest = (url, resourceType) => { // this handles when a style is switched diff --git a/src/version.ts b/src/version.ts index aedeb33a..c07025a8 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1 +1 @@ -export default '4.4.8'; \ No newline at end of file +export default '4.4.10-beta.1'; \ No newline at end of file