Backup user data to NativeStorage
This commit is contained in:
parent
675248dfd3
commit
9cb914512b
@ -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')
|
||||
|
@ -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);
|
@ -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");
|
||||
}
|
||||
|
@ -26,25 +26,37 @@
|
||||
|
||||
var totp = new jsOTP.totp();
|
||||
|
||||
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 = $('<div/>').html(keys[i]["label"]).html();
|
||||
var issuer = $('<div/>').text(keys[i]["issuer"]).html();
|
||||
$("#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>"
|
||||
+ "<p class=\"h6\">" + label + "</p>"
|
||||
+ "<div class=\"h3 code\" style=\"font-weight: bold;\">" + code + "</div>"
|
||||
+ "<p class=\"small\">" + issuer + "</p>"
|
||||
+ "</div>");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 = $('<div/>').html(keys[i]["label"]).html();
|
||||
var issuer = $('<div/>').text(keys[i]["issuer"]).html();
|
||||
$("#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>"
|
||||
+ "<p class=\"h6\">" + label + "</p>"
|
||||
+ "<div class=\"h3 code\" style=\"font-weight: bold;\">" + code + "</div>"
|
||||
+ "<p class=\"small\">" + issuer + "</p>"
|
||||
+ "</div>");
|
||||
}
|
||||
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() {
|
||||
|
@ -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?");
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user