Skip to content

Commit

Permalink
use server time when rotating qr codes
Browse files Browse the repository at this point in the history
  • Loading branch information
RedyAu authored and danmarsden committed May 17, 2024
1 parent 98c59f7 commit b0e3b68
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions js/password/attendance_QRCodeRotate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ class attendance_QRCodeRotate {
this.password = "";
this.qrCodeInstance = "";
this.qrCodeHTMLElement = "";
this.timeOffset = new Date();
}

start(sessionId, qrCodeHTMLElement, timerHTMLElement) {
start(sessionId, qrCodeHTMLElement, timerHTMLElement, serverTime) {
this.sessionId = sessionId;
this.qrCodeHTMLElement = qrCodeHTMLElement;
this.timerHTMLElement = timerHTMLElement;
this.timeOffset = new Date() - new Date(serverTime * 1000);
console.log(`Sync OK - Server time is ${new Date(serverTime * 1000)}\nClient's time is ${this.timeOffset < 0 ? 'late' : 'early'} by ${Math.abs(this.timeOffset)} milliseconds.`);
this.fetchAndRotate();
}

Expand All @@ -43,13 +46,17 @@ class attendance_QRCodeRotate {
this.timerHTMLElement.innerHTML = timeLeft;
}

serverTime() {
return Math.round((new Date().getTime() - this.timeOffset) / 1000);
}

startRotating() {
var parent = this;

setInterval(function() {
var found = Object.values(parent.password).find(function(element) {

if (element.expirytime > Math.round(new Date().getTime() / 1000)) {
if (element.expirytime > parent.serverTime()) {
return element;
}
});
Expand All @@ -58,7 +65,7 @@ class attendance_QRCodeRotate {
location.reload(true);
} else {
parent.changeQRCode(found.password);
parent.updateTimer(found.expirytime - Math.round(new Date().getTime() / 1000));
parent.updateTimer(found.expirytime - parent.serverTime());

}

Expand Down
2 changes: 1 addition & 1 deletion locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,7 @@ function attendance_renderqrcoderotate($session) {
echo '
<script type="text/javascript">
let qrCodeRotate = new attendance_QRCodeRotate();
qrCodeRotate.start(' . $session->id . ', document.getElementById("qrcode"), document.getElementById("rotate-time"));
qrCodeRotate.start(' . $session->id . ', document.getElementById("qrcode"), document.getElementById("rotate-time"), '. time() .');
</script>';
}

Expand Down

0 comments on commit b0e3b68

Please sign in to comment.