Check and refresh API key when needed
This commit is contained in:
parent
f1b7ce288c
commit
5434fe841e
@ -4,7 +4,8 @@
|
||||
|
||||
var $$ = Dom7;
|
||||
|
||||
// Run platform-specific setup code for Cordova or NW.js
|
||||
// Detect platform and run platform-specific setup code
|
||||
// for Cordova, NW.js, or the browser
|
||||
initPlatform();
|
||||
|
||||
var app = new Framework7({
|
||||
@ -44,10 +45,88 @@ router.on("pageInit", function (pagedata) {
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Check if there is a valid API key stored locally.
|
||||
* @param {function} callback A function that will be called with a boolean argument indicating if the key is valid.
|
||||
* @return {undefined}
|
||||
*/
|
||||
function checkKey(callback) {
|
||||
if (localStorage.getItem("key") === null) {
|
||||
callback(false);
|
||||
return;
|
||||
}
|
||||
|
||||
callAPI("checkkey", {
|
||||
key: localStorage.getItem("key")
|
||||
}, function (data) {
|
||||
var currentTimestamp = Math.floor(Date.now() / 1000);
|
||||
// Check if the key is valid and has at least one day before expiry
|
||||
// If someone leaves the app open longer than 24 hours and the key is
|
||||
// not renewed, there might be bugs.
|
||||
if (data.valid == true && data.expires > (currentTimestamp + (60 * 60 * 24))) {
|
||||
callback(true);
|
||||
return;
|
||||
}
|
||||
callback(false);
|
||||
}, function (msg) {
|
||||
callback(false);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to get a new API key using stored credentials.
|
||||
* @param {function} callback A function that will be called with a boolean argument indicating if the attempt was successful.
|
||||
* @return {undefined}
|
||||
*/
|
||||
function refreshKey(callback) {
|
||||
if (localStorage.getItem("username") === null || localStorage.getItem("password") === null) {
|
||||
callback(false);
|
||||
return;
|
||||
}
|
||||
|
||||
callAPI("getkey", {
|
||||
username: localStorage.getItem("username"),
|
||||
password: localStorage.getItem("password"),
|
||||
}, function (data) {
|
||||
localStorage.setItem("key", data.key);
|
||||
callback(true);
|
||||
}, function (msg) {
|
||||
callback(false);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
if (localStorage.getItem("configured") == null) {
|
||||
// Open the setup page
|
||||
router.navigate("/setup/0");
|
||||
} else {
|
||||
router.navigate("/home");
|
||||
checkKey(function (valid) {
|
||||
if (valid) {
|
||||
router.navigate("/home");
|
||||
|
||||
// Check and refresh API key as needed
|
||||
// Should mitigate key expiration if the app is left open for
|
||||
// a long time when the key is almost expired
|
||||
setInterval(function () {
|
||||
checkKey(function (valid) {
|
||||
if (!valid) {
|
||||
refreshKey(function (ok) {
|
||||
if (!ok) {
|
||||
router.navigate("/setup/0");
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}, 1000 * 60 * 5);
|
||||
} else {
|
||||
refreshKey(function (ok) {
|
||||
if (ok) {
|
||||
router.navigate("/home");
|
||||
} else {
|
||||
//router.navigate("/setup/0");
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user