From 9cb914512b4798970e547f6e5ab259ffbface277 Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Mon, 22 Oct 2018 13:44:26 -0600 Subject: [PATCH] Backup user data to NativeStorage --- www/js/accounts.js | 9 ++++++++ www/js/app.js | 13 ++++++++--- www/views/addotp.html | 1 + www/views/otp.html | 48 +++++++++++++++++++++++++---------------- www/views/settings.html | 10 +++++---- 5 files changed, 56 insertions(+), 25 deletions(-) diff --git a/www/js/accounts.js b/www/js/accounts.js index 41e0bdb..0179aa7 100644 --- a/www/js/accounts.js +++ b/www/js/accounts.js @@ -33,6 +33,15 @@ function saveaccounts(accounts) { NativeStorage.setItem("accounts", JSON.stringify(accounts)); } +function recoveraccounts(callback) { + NativeStorage.getItem("accounts", function (data) { + localStorage.setItem("accounts", data); + callback(true); + }, function () { + callback(false); + }); +} + /** * Switch to a different account. * @param {int} account The selected account index from localStorage.getItem('accounts') diff --git a/www/js/app.js b/www/js/app.js index 1fe914b..fabb41b 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -204,7 +204,6 @@ function displayNotifications(callback) { $.post(localStorage.getItem("syncurl"), { username: localStorage.getItem("username"), key: localStorage.getItem("key"), - password: localStorage.getItem("password"), action: "checknotifications" }, function (data) { if (data.status === 'OK') { @@ -312,11 +311,19 @@ document.addEventListener("deviceready", function () { minimumFetchInterval: 1, stopOnTerminate: false, startOnBoot: true, - forceReload: true + forceReload: false, + enableHeadless: true }); }); } else { - openscreen("setup1"); + // Try to recover data from NativeStorage back to localStorage + recoveraccounts(function (ok) { + if (ok) { + restartApplication(); + } else { + openscreen("setup1"); + } + }) } setTimeout(navigator.splashscreen.hide, 500); }, false); \ No newline at end of file diff --git a/www/views/addotp.html b/www/views/addotp.html index 2f26a3b..bc68b75 100644 --- a/www/views/addotp.html +++ b/www/views/addotp.html @@ -75,6 +75,7 @@ keys.push({"secret": key, "label": label, "issuer": issuer}); localStorage.setItem("otp", JSON.stringify(keys)); + NativeStorage.setItem("otp", JSON.stringify(keys)); navigator.notification.alert("2-factor key saved.", null, "Key added", 'Dismiss'); openscreen("otp"); } diff --git a/www/views/otp.html b/www/views/otp.html index 4ce8ddd..cac410f 100644 --- a/www/views/otp.html +++ b/www/views/otp.html @@ -26,27 +26,39 @@ var totp = new jsOTP.totp(); - var ls_text = localStorage.getItem("otp"); - var keys = []; - if (ls_text !== null && ls_text != "") { - var keys = JSON.parse(ls_text || "[]"); - if (keys.length > 0) { - $("#nokeys").css("display", "none"); - } - for (var i = 0; i < keys.length; i++) { - var code = totp.getOtp(keys[i]["secret"]); - // Escape HTML characters - var label = $('
').html(keys[i]["label"]).html(); - var issuer = $('
').text(keys[i]["issuer"]).html(); - $("#codelist").append("
" - + "" - + "

" + label + "

" - + "
" + code + "
" - + "

" + issuer + "

" - + "
"); + function load(jsontext) { + var keys = []; + if (jsontext !== null && jsontext != "") { + var keys = JSON.parse(jsontext || "[]"); + if (keys.length > 0) { + $("#nokeys").css("display", "none"); + } + for (var i = 0; i < keys.length; i++) { + var code = totp.getOtp(keys[i]["secret"]); + // Escape HTML characters + var label = $('
').html(keys[i]["label"]).html(); + var issuer = $('
').text(keys[i]["issuer"]).html(); + $("#codelist").append("
" + + "" + + "

" + label + "

" + + "
" + code + "
" + + "

" + issuer + "

" + + "
"); + } } } + var ls_text = localStorage.getItem("otp"); + if (ls_text === null || ls_text == "") { + // Recover from NativeStorage + NativeStorage.getItem("otp", function (data) { + localStorage.setItem("otp"); + load(data); + }); + } else { + load(ls_text); + } + function refreshCountdown() { var percent = ((30 - ((new Date).getSeconds() % 30)) / 30) * 100; $("#countdown").css("width", percent + "%"); diff --git a/www/views/settings.html b/www/views/settings.html index 676c421..c464117 100644 --- a/www/views/settings.html +++ b/www/views/settings.html @@ -71,10 +71,12 @@ localStorage.removeItem("syncurl"); localStorage.removeItem("key"); localStorage.removeItem("accounts"); - // force-reload app - navigator.notification.alert("All connection data and credentials erased.", function () { - restartApplication(); - }, "App Reset", 'Continue'); + NativeStorage.remove("accounts", function () { + // force-reload app + navigator.notification.alert("All connection data and credentials erased.", function () { + restartApplication(); + }, "App Reset", 'Continue'); + }); }, "Are you sure?"); }