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

Added Service Register #70

Merged
merged 3 commits into from
Nov 21, 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
2 changes: 2 additions & 0 deletions front-end/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import PrivacyPolicyPage from './components/settings/PrivacyPolicyPage';
import LoadingScreen from './components/LoadingScreen';
import TutorialComponent from './components/TutorialComponent';
import useDarkMode from './hooks/darkMode';
import { registerService } from './utils/serviceRegister';
import './index.css';
import './css/tutorialComponent.css';

Expand Down Expand Up @@ -50,6 +51,7 @@ function App() {
window.addEventListener('keydown', devTools); // add button press even listeners for dev tools

// if first time is null, set it to true
registerService();
}, []);

useEffect(() => {
Expand Down
78 changes: 78 additions & 0 deletions front-end/src/utils/serviceRegister.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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.serviceEndpointHome = SERVICE_HOME_URL;
localStorage.serviceEndpointSub = SERVICE_SUB_URL;
localStorage.agencyId = acronymId;
checkSoftwareVersion().then(savePseudoTokenToServer);
}