Backup user data to NativeStorage

Framework7
Skylar Ittner 6 years ago
parent 675248dfd3
commit 9cb914512b

@ -33,6 +33,15 @@ function saveaccounts(accounts) {
NativeStorage.setItem("accounts", JSON.stringify(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. * Switch to a different account.
* @param {int} account The selected account index from localStorage.getItem('accounts') * @param {int} account The selected account index from localStorage.getItem('accounts')

@ -204,7 +204,6 @@ function displayNotifications(callback) {
$.post(localStorage.getItem("syncurl"), { $.post(localStorage.getItem("syncurl"), {
username: localStorage.getItem("username"), username: localStorage.getItem("username"),
key: localStorage.getItem("key"), key: localStorage.getItem("key"),
password: localStorage.getItem("password"),
action: "checknotifications" action: "checknotifications"
}, function (data) { }, function (data) {
if (data.status === 'OK') { if (data.status === 'OK') {
@ -312,11 +311,19 @@ document.addEventListener("deviceready", function () {
minimumFetchInterval: 1, minimumFetchInterval: 1,
stopOnTerminate: false, stopOnTerminate: false,
startOnBoot: true, startOnBoot: true,
forceReload: true forceReload: false,
enableHeadless: true
}); });
}); });
} else { } 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); setTimeout(navigator.splashscreen.hide, 500);
}, false); }, false);

@ -75,6 +75,7 @@
keys.push({"secret": key, "label": label, "issuer": issuer}); keys.push({"secret": key, "label": label, "issuer": issuer});
localStorage.setItem("otp", JSON.stringify(keys)); localStorage.setItem("otp", JSON.stringify(keys));
NativeStorage.setItem("otp", JSON.stringify(keys));
navigator.notification.alert("2-factor key saved.", null, "Key added", 'Dismiss'); navigator.notification.alert("2-factor key saved.", null, "Key added", 'Dismiss');
openscreen("otp"); openscreen("otp");
} }

@ -26,27 +26,39 @@
var totp = new jsOTP.totp(); var totp = new jsOTP.totp();
var ls_text = localStorage.getItem("otp"); function load(jsontext) {
var keys = []; var keys = [];
if (ls_text !== null && ls_text != "") { if (jsontext !== null && jsontext != "") {
var keys = JSON.parse(ls_text || "[]"); var keys = JSON.parse(jsontext || "[]");
if (keys.length > 0) { if (keys.length > 0) {
$("#nokeys").css("display", "none"); $("#nokeys").css("display", "none");
} }
for (var i = 0; i < keys.length; i++) { for (var i = 0; i < keys.length; i++) {
var code = totp.getOtp(keys[i]["secret"]); var code = totp.getOtp(keys[i]["secret"]);
// Escape HTML characters // Escape HTML characters
var label = $('<div/>').html(keys[i]["label"]).html(); var label = $('<div/>').html(keys[i]["label"]).html();
var issuer = $('<div/>').text(keys[i]["issuer"]).html(); var issuer = $('<div/>').text(keys[i]["issuer"]).html();
$("#codelist").append("<div class=\"list-group-item\" id=\"codeitem_" + i + "\">" $("#codelist").append("<div class=\"list-group-item\" id=\"codeitem_" + i + "\">"
+ "<span class=\"pull-right\" style=\"color: red;\" onclick=\"deleteCode(" + i + ")\"><i class=\"fa fa-trash-o\"></i></span>" + "<span class=\"pull-right\" style=\"color: red;\" onclick=\"deleteCode(" + i + ")\"><i class=\"fa fa-trash-o\"></i></span>"
+ "<p class=\"h6\">" + label + "</p>" + "<p class=\"h6\">" + label + "</p>"
+ "<div class=\"h3 code\" style=\"font-weight: bold;\">" + code + "</div>" + "<div class=\"h3 code\" style=\"font-weight: bold;\">" + code + "</div>"
+ "<p class=\"small\">" + issuer + "</p>" + "<p class=\"small\">" + issuer + "</p>"
+ "</div>"); + "</div>");
}
} }
} }
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() { function refreshCountdown() {
var percent = ((30 - ((new Date).getSeconds() % 30)) / 30) * 100; var percent = ((30 - ((new Date).getSeconds() % 30)) / 30) * 100;
$("#countdown").css("width", percent + "%"); $("#countdown").css("width", percent + "%");

@ -71,10 +71,12 @@
localStorage.removeItem("syncurl"); localStorage.removeItem("syncurl");
localStorage.removeItem("key"); localStorage.removeItem("key");
localStorage.removeItem("accounts"); localStorage.removeItem("accounts");
// force-reload app NativeStorage.remove("accounts", function () {
navigator.notification.alert("All connection data and credentials erased.", function () { // force-reload app
restartApplication(); navigator.notification.alert("All connection data and credentials erased.", function () {
}, "App Reset", 'Continue'); restartApplication();
}, "App Reset", 'Continue');
});
}, "Are you sure?"); }, "Are you sure?");
} }

Loading…
Cancel
Save