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

Truncate session and send location authorization in track requests #115

Merged
merged 1 commit into from
Aug 3, 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
9 changes: 9 additions & 0 deletions src/api/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ class TrackAPI {
accuracy = deviceLocation.accuracy;
}

// location authorization
let locationAuthorization;
try {
locationAuthorization = await Navigator.getPermissionStatus();
} catch (err: any) {
Logger.warn(`Location authorization error: ${err.message}`);
}

// user indentification fields
const userId = params.userId || Storage.getItem(Storage.USER_ID);
const deviceId = params.deviceId || Device.getDeviceId();
Expand All @@ -52,6 +60,7 @@ class TrackAPI {

const body = {
...params,
locationAuthorization,
accuracy,
description,
deviceId,
Expand Down
4 changes: 2 additions & 2 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Storage from './storage';
const SESSION_TIMEOUT_SECS = 300; // 5 mins

const isValid = (sessionId: string): boolean => {
const now = Date.now() / 1000;
const now = Math.trunc(Date.now() / 1000);
const session = Number.parseInt(sessionId);
const diff = Math.abs(now - session);
return diff < SESSION_TIMEOUT_SECS;
Expand All @@ -19,7 +19,7 @@ class Session {
}

// create new session if does not already exist or expired
const newSessionId = (Date.now() / 1000).toString(); // unix ts in seconds
const newSessionId = Math.trunc(Date.now() / 1000).toString(); // unix ts in seconds
Storage.setItem(Storage.SESSION_ID, newSessionId);
return newSessionId;
}
Expand Down
Loading