Add account switching

Framework7
Skylar Ittner 6 years ago
parent a148a34597
commit fb333ce6bd

@ -17,6 +17,7 @@
<script src="js/polyfills.js"></script>
<script src="js/jquery-3.2.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/accounts.js"></script>
<script src="js/app.js"></script>
</head>
<body>

@ -0,0 +1,131 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
userinfo = null;
theme = {"title": "Business", "color": "#ffffff", "bgcolor": ""};
accountid = 0;
/**
* Check if the accounts configuration seems valid.
* @returns {Boolean} true if valid, otherwise false
*/
function isconfigvalid() {
if (localStorage.getItem("accounts") !== null) {
if (JSON.parse(localStorage.getItem("accounts")).length > 0) {
return true;
}
}
return false;
}
function getaccounts() {
if (isconfigvalid()) {
return JSON.parse(localStorage.getItem("accounts"));
}
return [];
}
function saveaccounts(accounts) {
localStorage.setItem("accounts", JSON.stringify(accounts));
}
/**
* Switch to a different account.
* @param {int} account The selected account index from localStorage.getItem('accounts')
* @returns {undefined}
*/
function switchaccount(account) {
// If there isn't an accounts list yet, this shouldi take us back to the setup
if (!isconfigvalid()) {
restartApplication();
}
var accounts = getaccounts();
// Selected account doesn't exist, choose the first one instead
if (typeof accounts[account] === 'undefined') {
account = 0;
}
var accountinfo = accounts[account];
localStorage.setItem("username", accountinfo['username']);
localStorage.setItem("password", accountinfo['password']);
localStorage.setItem("syncurl", accountinfo['syncurl']);
localStorage.setItem("key", accountinfo['key']);
accountid = account;
}
/**
* Add a Business Apps account.
* @param {String} username
* @param {String} password
* @param {String} url
* @param {String} key
* @returns {Number} The index of the new account
*/
function addaccount(username, password, url, key) {
var accounts = getaccounts();
accounts.push({"username": username, "password": password, "syncurl": url, "key": key});
saveaccounts(accounts);
return accounts.length - 1;
}
/**
* Remove a Business Apps account.
* @param {Number} Account index
*/
function rmaccount(id) {
var accounts = getaccounts();
accounts.splice(id, 1);
saveaccounts(accounts);
if (id == accountid) {
switchaccount(0);
}
}
/**
* Fetch user info (name, email, etc) from the server and save to the global
* variable `userinfo`.
* @param function callback An optional function to run if/when the request
* succeeds.
*/
function getuserinfo(callback) {
$(".loading-text").text("Loading account...");
if (localStorage.getItem("username") === null
|| localStorage.getItem("password") === null
|| localStorage.getItem("syncurl") === null
|| localStorage.getItem("key") === null) {
switchaccount(0);
}
$.post(localStorage.getItem("syncurl"), {
username: localStorage.getItem("username"),
key: localStorage.getItem("key"),
password: localStorage.getItem("password"),
action: "user_info"
}, function (data) {
if (data.status === 'OK') {
$(".loading-text").text("Loading...");
userinfo = data.info;
if (typeof callback == 'function') {
callback();
}
} else {
navigator.notification.alert(data.msg, null, "Error", 'Dismiss');
openscreen("homeloaderror");
}
}, "json").fail(function () {
navigator.notification.alert("Could not connect to the server. Try again later.", null, "Error", 'Dismiss');
openscreen("homeloaderror");
});
}
/**
* Set a new password for the current account
* @param {String} newpass
* @returns {undefined}
*/
function passwd(newpass) {
var accounts = getaccounts();
accounts[accountid]["password"] = newpass;
saveaccounts(accounts);
}

@ -2,8 +2,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
userinfo = null;
/**
* Switches the app to the given screen.
* @param {String} screenname The name of the screen to show.
@ -29,35 +27,6 @@ function openscreen(screenname, effect) {
currentscreen = screenname;
}
/**
* Fetch user info (name, email, etc) from the server and save to the global
* variable `userinfo`.
* @param function callback An optional function to run if/when the request
* succeeds.
*/
function getuserinfo(callback) {
$(".loading-text").text("Loading account...");
$.post(localStorage.getItem("syncurl"), {
username: localStorage.getItem("username"),
key: localStorage.getItem("key"),
password: localStorage.getItem("password"),
action: "user_info"
}, function (data) {
if (data.status === 'OK') {
$(".loading-text").text("Loading...");
userinfo = data.info;
if (typeof callback == 'function') {
callback();
}
} else {
navigator.notification.alert(data.msg, null, "Error", 'Dismiss');
openscreen("homeloaderror");
}
}, "json").fail(function () {
navigator.notification.alert("Could not connect to the server. Try again later.", null, "Error", 'Dismiss');
openscreen("homeloaderror");
});
}
function openfragment(fragment, target, effect) {
if (effect === 'FADE') {
@ -110,11 +79,11 @@ function setnavbartitle(title, showarrow, backscreen) {
/**
* Set the navbar.
* @param String,boolean type false if no navbar, "home" for the home screen,
* @param {String,boolean} type false if no navbar, "home" for the home screen,
* "settings" for settings, "app" for a custom title, or anything else for
* a simple one.
* @param String screentitle The title to show if type == "app"
* @param String returnscreen Where to go back to. Defaults to "home".
* @param {String} screentitle The title to show if type == "app"
* @param {String} returnscreen Where to go back to. Defaults to "home".
* @returns {undefined}
*/
function setnavbar(type, screentitle, returnscreen) {
@ -231,7 +200,7 @@ function restartApplication() {
// Handle back button to close things
document.addEventListener("backbutton", function (event) {
if (localStorage.getItem("setupcomplete")) {
if (isconfigvalid()) {
if ($("#appframe").length && historyctr > 0) {
console.log("going back");
var iframe = document.getElementById("appframe");
@ -264,7 +233,7 @@ document.addEventListener("deviceready", function () {
$(this).parent().fadeOut("slow");
});
if (localStorage.getItem("setupcomplete")) {
if (isconfigvalid()) {
getuserinfo(function () {
openscreen("home");
});

@ -0,0 +1,61 @@
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<div class="circle-btn btn btn-light-blue" onclick="openscreen('setup1', 'FADE');">
<img src="icons/ic_add.svg" />
</div>
<div id="noaccounts">
<div class="app-dock-container">
<div class="app-dock" id="app-dock">
<div style="margin-top: 50px; text-align: center; font-size: 120%;">
<img src="img/noaccounts.svg" alt="" style="max-width: 80%; max-height: 25%;" />
<br /><br />
<p style="max-width: 80%; margin: 0 auto;">You haven't added any Business Apps accounts yet. Press <i class="fa fa-plus"></i> to add one.</p>
</div>
</div>
</div>
</div>
<div class="list-group" id="accountlist">
</div>
<script>
setnavbar("app", "Accounts", "settings");
var accounts = getaccounts();
if (accounts.length > 0) {
$("#noaccounts").css("display", "none");
}
for (var i = 0; i < accounts.length; i++) {
// Escape HTML characters
var username = $('<div/>').html(accounts[i]["username"]).html();
var syncurl = $('<div/>').html(accounts[i]["syncurl"].replace("/mobile/index.php", "")).html();
var synckey = accounts[i]["key"];
var stars = "";
for (var j = 0; j < synckey.length - 6; j++) {
stars += "*";
}
synckey = $('<div/>').html(key.slice(0, 3) + stars + key.slice(-3)).html();
$("#accountlist").append("<div class=\"list-group-item\" id=\"accountitem_" + i + "\">"
+ "<span class=\"pull-right\" style=\"color: red;\" onclick=\"deleteAccount(" + i + ")\"><i class=\"fa fa-trash-o\"></i></span>"
+ "<div onclick=\"switchAccount(" + i + ")\" class=\"h3\" style=\"font-weight: bold;\"><i class=\"fa fa-user\"></i> " + username + "</div>"
+ "<p onclick=\"switchAccount(" + i + ")\">"
+ "<i class=\"fa fa-key\"></i> Pairing Code: " + synckey + "<br />"
+ "<i class=\"fa fa-server\"></i> Server: " + syncurl + "</p>"
+ "</div>");
}
function deleteAccount(id) {
navigator.notification.confirm("Really delete account? You'll need to re-add the account to use it on this device again.", function (result) {
if (result != 1) {
return;
}
rmaccount(id);
openscreen("accounts");
}, "Delete " + accounts[id]['username'] + "?");
}
function switchAccount(id) {
switchaccount(id);
openscreen("home");
navigator.notification.alert("Successfully switched accounts.", null, "Switched", 'OK');
}
</script>

@ -40,7 +40,7 @@
});
}
if (localStorage.getItem('setupcomplete')) {
if (isconfigvalid()) {
setnavbar("home");
loadapps();
} else {

@ -7,6 +7,9 @@
<i class="fa fa-key"></i> Pairing Code: <span id="pairingkey">None</span><br />
<i class="fa fa-server"></i> Server: <span id="syncurl">None</span>
</div>
<div class="list-group-item" onclick="openscreen('accounts', 'FADE');">
<b>Manage and switch accounts</b>
</div>
</div>
<div class="list-group">
@ -15,8 +18,8 @@
<p>Enter your new password if you changed it from AccountHub.</p>
</div>
<div class="list-group-item" onclick="deleteall()">
<b>Log out</b>
<p>Forget all cached account data (including sync key) and open the setup tool.</p>
<b>Disconnect All Accounts</b>
<p>Forget all account data for all connected accounts and open the setup tool.</p>
</div>
</div>
@ -63,13 +66,13 @@
return;
}
// Wipe localStorage
localStorage.removeItem("setupcomplete");
localStorage.removeItem("username");
localStorage.removeItem("password");
localStorage.removeItem("syncurl");
localStorage.removeItem("key");
localStorage.removeItem("accounts");
// force-reload app
navigator.notification.alert("Connection data and credentials erased.", function () {
navigator.notification.alert("All connection data and credentials erased.", function () {
restartApplication();
}, "App Reset", 'Continue');
}, "Are you sure?");
@ -90,7 +93,7 @@
}
/**
* Prompts the user to enter their password, then checks the password and
* Prompts the user to enter their password, then checks the password and
* saves or displays an error.
* @returns {undefined}
*/
@ -105,7 +108,7 @@
}, function (data) {
if (data.status === 'OK') {
localStorage.setItem("password", results.input1);
localStorage.setItem("setupcomplete", true);
passwd(results.input1);
navigator.notification.alert("Saved password updated.", function () {
// Reload app just to be safe
restartApplication();
@ -133,7 +136,7 @@
cordova.getAppVersion.getVersionCode(function (version) {
$('#app_version_code').text(version);
});
if (localStorage.getItem("username")) {
$("#username").text(localStorage.getItem("username"));
}

@ -4,12 +4,15 @@
<br />
<div class="panel panel-blue">
<div class="panel-heading">
<h3 class="panel-title">Setup</h3>
<h3 class="panel-title" id="setuptitle">Add Account</h3>
</div>
<div class="panel-body">
<p>Welcome! There's a few things we need to do to get everything ready.
<br /><br />
Open AccountHub on another device and go to your account settings. Generate a mobile sync code, then press the button below to scan it.
<p>
<span class="firstrun-only" style="display: none;">
Welcome! There's a few things we need to do to get everything ready.
<br /><br />
</span>
Open AccountHub on another device and go to Sync settings. Generate a mobile sync code, then press the button below to scan it.
</p>
<span class="btn btn-primary" onclick="scanCode()" id="scancodebtn">
<i class="fa fa-qrcode"></i> Scan Code
@ -38,6 +41,16 @@
</div>
<script>
setupusername = "";
setuppassword = "";
setupsynckey = "";
setupsyncurl = "";
if (localStorage.getItem("firstrun") === null) {
$("#setuptitle").text("Setup");
$(".firstrun-only").css("display", "");
}
$('#use-security').click(function () {
if (this.checked) {
$('#protocol-select').text("https://");
@ -83,29 +96,29 @@
function manualsetup() {
if ($('#syncurl').val().toLowerCase().startsWith("http")) {
var portal = $('#syncurl').val();
var syncurl = $('#syncurl').val();
} else {
var portal = $('#protocol-select').text() + $('#syncurl').val();
var syncurl = $('#protocol-select').text() + $('#syncurl').val();
}
var username = $('#username').val();
var key = $('#key').val().replace(/\s+/g, '');
checkAndSave(portal, username, key);
checkAndSave(syncurl, username, key);
}
function manualshow() {
$('#manual_setup').css('display', 'block');
}
function checkAndSave(portal, username, key) {
$.post(portal, {
function checkAndSave(syncurl, username, key) {
$.post(syncurl, {
username: username,
key: key,
action: "check_key"
}, function (data) {
if (data.status === 'OK') {
localStorage.setItem("username", username);
localStorage.setItem("syncurl", portal);
localStorage.setItem("key", key);
setupusername = username;
setupsyncurl = syncurl;
setupsynckey = key;
openscreen("setup2");
} else {
navigator.notification.alert(data.msg, null, "Error", 'Dismiss');
@ -127,10 +140,10 @@
}
var url = result.text.replace("bizsync://", "");
var parts = url.split("/");
var portal = parts[0].replace(/\\/g, "/");
var syncurl = parts[0].replace(/\\/g, "/");
var username = parts[1];
var key = parts[2];
checkAndSave(portal, username, key);
checkAndSave(syncurl, username, key);
}
},
function (error) {

@ -19,17 +19,19 @@
<script>
function savePassword() {
$.post(localStorage.getItem("syncurl"), {
username: localStorage.getItem("username"),
key: localStorage.getItem("key"),
$.post(setupsyncurl, {
username: setupusername,
key: setupsynckey,
password: $('#passbox').val(),
action: "check_password"
}, function (data) {
if (data.status === 'OK') {
localStorage.setItem("password", $('#passbox').val());
localStorage.setItem("setupcomplete", true);
navigator.notification.alert("Setup complete!", null, "Success", 'Continue');
openscreen("home");
setuppassword = $('#passbox').val();
var accid = addaccount(setupusername, setuppassword, setupsyncurl, setupsynckey);
switchaccount(accid);
localStorage.setItem("firstrun", "1");
navigator.notification.alert("Account connected!", null, "Success", 'Continue');
restartApplication();
} else {
navigator.notification.alert(data.msg, null, "Error", 'Dismiss');
}

Loading…
Cancel
Save