From eb64269927960b9e8543d50e986e9b6d4868fc5b Mon Sep 17 00:00:00 2001 From: tamaina Date: Sat, 4 Sep 2021 18:09:53 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=B5=E3=82=A4=E3=83=B3=E3=82=A2=E3=83=83?= =?UTF-8?q?=E3=83=97=E3=83=BB=E3=83=AD=E3=82=B0=E3=82=A2=E3=82=A6=E3=83=88?= =?UTF-8?q?=E5=91=A8=E3=82=8A=E3=81=AE=E6=80=AA=E3=81=97=E3=81=84=E6=8C=99?= =?UTF-8?q?=E5=8B=95=E3=82=92=E4=BF=AE=E6=AD=A3=20(#7750)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix #7749 * fix * fix logout behavior when there is no push subscription * fix logout behavior when there is no push subscription 2 * clean up service worker registration * fix lint * remove submodules --- src/client/account.ts | 38 ++++++++++++++++++++---------- src/client/components/signin.vue | 6 +++-- src/client/components/signup.vue | 4 ++-- src/client/pages/welcome.setup.vue | 2 +- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/client/account.ts b/src/client/account.ts index ee1d84549..e469bae5a 100644 --- a/src/client/account.ts +++ b/src/client/account.ts @@ -1,4 +1,4 @@ -import { get, set } from '@client/scripts/idb-proxy'; +import { del, get, set } from '@client/scripts/idb-proxy'; import { reactive } from 'vue'; import { apiUrl } from '@client/config'; import { waiting } from '@client/os'; @@ -26,21 +26,33 @@ export async function signout() { //#region Remove account const accounts = await getAccounts(); accounts.splice(accounts.findIndex(x => x.id === $i.id), 1); - set('accounts', accounts); + + if (accounts.length > 0) await set('accounts', accounts); + else await del('accounts'); //#endregion - //#region Remove push notification registration + //#region Remove service worker registration try { - const registration = await navigator.serviceWorker.ready; - const push = await registration.pushManager.getSubscription(); - if (!push) return; - await fetch(`${apiUrl}/sw/unregister`, { - method: 'POST', - body: JSON.stringify({ - i: $i.token, - endpoint: push.endpoint, - }), - }); + if (navigator.serviceWorker.controller) { + const registration = await navigator.serviceWorker.ready; + const push = await registration.pushManager.getSubscription(); + if (push) { + await fetch(`${apiUrl}/sw/unregister`, { + method: 'POST', + body: JSON.stringify({ + i: $i.token, + endpoint: push.endpoint, + }), + }); + } + } + + if (accounts.length === 0) { + await navigator.serviceWorker.getRegistrations() + .then(registrations => { + return Promise.all(registrations.map(registration => registration.unregister())); + }); + } } catch (e) {} //#endregion diff --git a/src/client/components/signin.vue b/src/client/components/signin.vue index 0094038fb..c051288d0 100755 --- a/src/client/components/signin.vue +++ b/src/client/components/signin.vue @@ -111,7 +111,9 @@ export default defineComponent({ onLogin(res) { if (this.autoSet) { - login(res.i); + return login(res.i); + } else { + return; } }, @@ -144,7 +146,7 @@ export default defineComponent({ }); }).then(res => { this.$emit('login', res); - this.onLogin(res); + return this.onLogin(res); }).catch(err => { if (err === null) return; os.dialog({ diff --git a/src/client/components/signup.vue b/src/client/components/signup.vue index d584b9720..d33227411 100644 --- a/src/client/components/signup.vue +++ b/src/client/components/signup.vue @@ -178,14 +178,14 @@ export default defineComponent({ 'hcaptcha-response': this.hCaptchaResponse, 'g-recaptcha-response': this.reCaptchaResponse, }).then(() => { - os.api('signin', { + return os.api('signin', { username: this.username, password: this.password }).then(res => { this.$emit('signup', res); if (this.autoSet) { - login(res.i); + return login(res.i); } }); }).catch(() => { diff --git a/src/client/pages/welcome.setup.vue b/src/client/pages/welcome.setup.vue index de844ece1..d0091bef6 100644 --- a/src/client/pages/welcome.setup.vue +++ b/src/client/pages/welcome.setup.vue @@ -53,7 +53,7 @@ export default defineComponent({ username: this.username, password: this.password, }).then(res => { - login(res.i); + return login(res.token); }).catch(() => { this.submitting = false;