Use localStorage

This commit is contained in:
2022-08-29 14:31:11 +02:00
parent 01a7bed1c9
commit 494e297224
3 changed files with 63 additions and 16 deletions

View File

@ -26,15 +26,37 @@ export class App {
this.handleUrl(event.newURL); this.handleUrl(event.newURL);
}); });
this.handleUrl(document.URL); const domainName = window.localStorage['domainName'];
const sessionToken = window.localStorage['sessionToken'];
if (domainName && sessionToken) {
USIMP.Domain.fromName(domainName).then((domain) => {
const session = new USIMP.Session(domain);
session.chooseDomainServer().then(() => {
session.token = sessionToken;
session.ping().then(() => {
this.session = session;
this.defaultLocation = '/';
this.handleUrl(document.URL);
})
});
}, (error) => {
if (error instanceof DOMException) {
throw new Error("Connection timed out");
} else {
throw new Error("Invalid USIMP domain");
}
});
} else {
this.handleUrl(document.URL);
}
} }
close() { quit() {
if (this.session) this.session.close(); if (this.session) this.session.close().then();
} }
sleep() { sleep() {
if (this.session) this.session.sleep(); if (this.session) this.session.sleep().then();
} }
async wakeup() { async wakeup() {
@ -52,7 +74,7 @@ export class App {
} }
handleHash(hash: string) { handleHash(hash: string) {
if (hash[0] === '#') hash = hash.substr(1); if (hash[0] === '#') hash = hash.substring(1);
const defaultCase = () => { const defaultCase = () => {
history.replaceState(null, document.title, `#${this.defaultLocation}`); history.replaceState(null, document.title, `#${this.defaultLocation}`);
@ -110,7 +132,7 @@ export class App {
if (response.status === "success") { if (response.status === "success") {
this.session = session; this.session = session;
this.defaultLocation = "/"; this.defaultLocation = "/";
this.setHash("/"); return true;
} else { } else {
console.error(response.error); console.error(response.error);
throw new Error(response.error.message || response.error.code); throw new Error(response.error.message || response.error.code);
@ -188,6 +210,8 @@ export class App {
<input name="account" placeholder="Account name" type="text" required/> <input name="account" placeholder="Account name" type="text" required/>
<input name="domain" placeholder="Domain" type="text" pattern="([a-zA-Z0-9_-]+\\.)+[a-zA-Z]{2,}" required/> <input name="domain" placeholder="Domain" type="text" pattern="([a-zA-Z0-9_-]+\\.)+[a-zA-Z]{2,}" required/>
<input name="password" placeholder="Password" type="password" required/> <input name="password" placeholder="Password" type="password" required/>
<label><input name="saveAccountName" type="checkbox"/> Save Account Name</label>
<label><input name="keepSession" type="checkbox"/> Keep me signed in</label>
<button type="submit">Login</button> <button type="submit">Login</button>
</form>`; </form>`;
@ -204,8 +228,21 @@ export class App {
} }
try { try {
const accountName: string = form['account'].value;
const domainName: string = form['domain'].value;
const keepSession: boolean = form['keepSession'].checked;
const saveAccountName: boolean = form['saveAccountName'].checked;
try { try {
await this.login(form['account'].value, form['domain'].value, form['password'].value); if (await this.login(accountName, domainName, form['password'].value)) {
window.localStorage.clear();
if (keepSession || saveAccountName)
window.localStorage['domainName'] = domainName;
if (keepSession)
window.localStorage['sessionToken'] = this.session?.token;
if (saveAccountName)
window.localStorage['accountName'] = accountName;
this.setHash("/");
}
} finally { } finally {
for (const d of form.getElementsByTagName("div")) form.removeChild(d); for (const d of form.getElementsByTagName("div")) form.removeChild(d);
for (const e of form.getElementsByTagName("input")) e.disabled = false; for (const e of form.getElementsByTagName("input")) e.disabled = false;
@ -231,19 +268,29 @@ export class App {
const domain = document.getElementsByName("domain")[0]; const domain = document.getElementsByName("domain")[0];
if (!domain) throw new Error("Element name=domain not found"); if (!domain) throw new Error("Element name=domain not found");
domain.addEventListener("input", () => {
domain.addEventListener("input", (event) => {
domain.removeAttribute("invalid"); domain.removeAttribute("invalid");
}); });
const account = document.getElementsByName("account")[0]; const account = document.getElementsByName("account")[0];
if (!account) throw new Error("Element name=account not found"); if (!account) throw new Error("Element name=account not found");
account.addEventListener("input", () => {
account.addEventListener("input", (event) => {
account.removeAttribute("invalid"); account.removeAttribute("invalid");
}); });
account.focus(); const password = document.getElementsByName("password")[0];
if (!password) throw new Error("Element name=password not found");
const accountName = window.localStorage['accountName'];
const domainName = window.localStorage['domainName'];
if (accountName && domainName) {
form['account'].value = accountName;
form['domain'].value = domainName;
form['saveAccountName'].checked = true;
password.focus();
} else {
account.focus();
}
} }
setupMain() { setupMain() {

View File

@ -28,7 +28,7 @@ window.addEventListener("DOMContentLoaded", () => {
} }
window.addEventListener("beforeunload", () => { window.addEventListener("beforeunload", () => {
locutus.close(); locutus.quit();
}); });
}); });

View File

@ -1,10 +1,10 @@
{ {
"compilerOptions": { "compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */ /* Visit https://aka.ms/tsconfig.json to read more about this file */
"target": "ES2020", "target": "ESNext",
"module": "ES2020", "module": "ESNext",
"outDir": "dest/www/res/scripts", "outDir": "dest/www/res/scripts",
"rootDir": "src", "rootDir": "src/",
"strict": true, "strict": true,
"noImplicitAny": true, "noImplicitAny": true,
"strictNullChecks": true, "strictNullChecks": true,