generated from agiledev-students-fall2023/generic-project-repository
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from agiledev-students-fall2023/map-feature-3
Map feature 3
- Loading branch information
Showing
3 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import axios from 'axios'; | ||
|
||
const SERVICE_HOME_URL = 'https://passiogo.com/'; | ||
const SERVICE_SUB_URL = 'https://nyu.passiogo.com'; | ||
const acronymId = 1007; | ||
const FALLBACK = { | ||
softwareVer: 110, | ||
}; | ||
|
||
// Get service endpoint version. | ||
async function checkSoftwareVersion() { | ||
try { | ||
const response = await axios.get(`${SERVICE_HOME_URL}goServices.php?goWebVer=1`); | ||
const data = response.data; | ||
|
||
// console.log('Version', data ? data.ver : data); | ||
localStorage.softwareVer = data.ver; | ||
} catch (error) { | ||
console.error('Error fetching software version:', error); | ||
localStorage.softwareVer = FALLBACK.softwareVer; | ||
} | ||
} | ||
|
||
// Generate a pseudo UUID | ||
function generatePseudoUUID() { | ||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { | ||
var r = (Math.random() * 16) | 0, | ||
v = c === 'x' ? r : (r & 0x3) | 0x8; | ||
return v.toString(16); | ||
}); | ||
} | ||
|
||
// Send a pseudo token to the service endpoint | ||
function savePseudoTokenToServer() { | ||
let token = localStorage.currentToken || `pseudo${localStorage.softwareVer}_${generatePseudoUUID()}`; | ||
saveTokenToServer(token); | ||
} | ||
|
||
// Send the current token to the service endpoint | ||
function saveTokenToServer(currentToken) { | ||
if (!currentToken) { | ||
currentToken = localStorage.currentToken || ''; | ||
} else { | ||
localStorage.currentToken = currentToken; | ||
} | ||
|
||
const queryParams = new URLSearchParams({ | ||
register: '1', | ||
deviceId: localStorage.deviceId, | ||
token: currentToken, | ||
platform: 'web', | ||
buildNo: localStorage.softwareVer, | ||
oldToken: localStorage.currentToken || '', | ||
}); | ||
|
||
const url = `${SERVICE_SUB_URL}/goServices.php?${queryParams.toString()}`; | ||
|
||
axios | ||
.get(url) | ||
.then((response) => { | ||
const data = response.data; | ||
// if (parseInt(localStorage.deviceId) !== data.deviceId) { | ||
// console.log(`Changed deviceId ${localStorage.deviceId} to ${data.deviceId}`); | ||
// } | ||
localStorage.deviceId = data.deviceId; | ||
localStorage.removeItem('currentToken'); | ||
}) | ||
.catch((error) => { | ||
console.error('Error while saving token to server:', error); | ||
}); | ||
} | ||
|
||
export function registerService() { | ||
localStorage.acronymId = acronymId; | ||
checkSoftwareVersion().then(savePseudoTokenToServer); | ||
} |