code refactor

This commit is contained in:
MaysWind
2025-01-05 21:39:39 +08:00
parent 49f1f3c86b
commit 6dc0ebcac6
22 changed files with 279 additions and 229 deletions
+17 -9
View File
@@ -43,6 +43,14 @@ import { useTransactionsStore } from '@/stores/transaction.js';
import logger from '@/lib/logger.ts';
import webauthn from '@/lib/webauthn.js';
import {
getUserAppLockState,
encryptToken,
decryptToken,
isCorrectPinCode,
saveWebAuthnConfig,
clearWebAuthnConfig
} from '@/lib/userstate.ts';
export default {
data() {
@@ -81,12 +89,12 @@ export default {
self.$showLoading();
webauthn.registerCredential(
self.$user.getUserAppLockState(),
getUserAppLockState(),
self.userStore.currentUserBasicInfo,
).then(({ id }) => {
self.$hideLoading();
self.$user.saveWebAuthnConfig(id);
saveWebAuthnConfig(id);
self.settingsStore.setEnableApplicationLockWebAuthn(true);
self.$toast('You have enabled WebAuthn successfully');
}).catch(error => {
@@ -106,11 +114,11 @@ export default {
self.isEnableApplicationLockWebAuthn = false;
self.settingsStore.setEnableApplicationLockWebAuthn(false);
self.$user.clearWebAuthnConfig();
clearWebAuthnConfig();
});
} else {
self.settingsStore.setEnableApplicationLockWebAuthn(false);
self.$user.clearWebAuthnConfig();
clearWebAuthnConfig();
}
}
},
@@ -144,12 +152,12 @@ export default {
return;
}
this.$user.encryptToken(user.username, pinCode);
encryptToken(user.username, pinCode);
this.settingsStore.setEnableApplicationLock(true);
this.transactionsStore.saveTransactionDraft();
this.settingsStore.setEnableApplicationLockWebAuthn(false);
this.$user.clearWebAuthnConfig();
clearWebAuthnConfig();
this.showInputPinCodeSheetForEnable = false;
},
@@ -164,17 +172,17 @@ export default {
return;
}
if (!this.$user.isCorrectPinCode(pinCode)) {
if (!isCorrectPinCode(pinCode)) {
this.$alert('Incorrect PIN code');
return;
}
this.$user.decryptToken();
decryptToken();
this.settingsStore.setEnableApplicationLock(false);
this.transactionsStore.saveTransactionDraft();
this.settingsStore.setEnableApplicationLockWebAuthn(false);
this.$user.clearWebAuthnConfig();
clearWebAuthnConfig();
this.showInputPinCodeSheetForDisable = false;
}
+2 -1
View File
@@ -210,6 +210,7 @@ import { useOverviewStore } from '@/stores/overview.js';
import { DateRange } from '@/core/datetime.ts';
import { TemplateType } from '@/core/template.ts';
import { formatUnixTime } from '@/lib/datetime.ts';
import { isUserLogined, isUserUnlocked } from '@/lib/userstate.ts';
export default {
props: [
@@ -269,7 +270,7 @@ export default {
created() {
const self = this;
if (self.$user.isUserLogined() && self.$user.isUserUnlocked()) {
if (isUserLogined() && isUserUnlocked()) {
self.loading = true;
self.overviewStore.loadTransactionOverview({
+2 -1
View File
@@ -184,6 +184,7 @@ import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { CategoryType } from '@/core/category.ts';
import { getNameByKeyValue, categorizedArrayToPlainArray } from '@/lib/common.ts';
import { isUserLogined } from '@/lib/userstate.ts';
import { setExpenseAndIncomeAmountColor } from '@/lib/ui/common.ts';
export default {
@@ -307,7 +308,7 @@ export default {
user: self.user,
presetCategories: presetCategories
}).then(response => {
if (!self.$user.isUserLogined()) {
if (!isUserLogined()) {
self.submitting = false;
self.$hideLoading();
+11 -5
View File
@@ -76,6 +76,12 @@ import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { APPLICATION_LOGO_PATH } from '@/consts/asset.ts';
import logger from '@/lib/logger.ts';
import webauthn from '@/lib/webauthn.js';
import {
unlockTokenByWebAuthn,
unlockTokenByPinCode,
hasWebAuthnConfig,
getWebAuthnCredentialId
} from '@/lib/userstate.ts';
import { setExpenseAndIncomeAmountColor } from '@/lib/ui/common.ts';
import { isModalShowing } from '@/lib/ui/mobile.js';
@@ -101,7 +107,7 @@ export default {
},
isWebAuthnAvailable() {
return this.settingsStore.appSettings.applicationLockWebAuthn
&& this.$user.getWebAuthnCredentialId()
&& hasWebAuthnConfig()
&& webauthn.isSupported();
},
currentLanguageCode() {
@@ -116,7 +122,7 @@ export default {
const self = this;
const router = self.f7router;
if (!self.settingsStore.appSettings.applicationLockWebAuthn || !self.$user.getWebAuthnCredentialId()) {
if (!self.settingsStore.appSettings.applicationLockWebAuthn || !hasWebAuthnConfig()) {
self.$toast('WebAuthn is not enabled');
return;
}
@@ -130,11 +136,11 @@ export default {
webauthn.verifyCredential(
self.userStore.currentUserBasicInfo,
self.$user.getWebAuthnCredentialId()
getWebAuthnCredentialId()
).then(({ id, userName, userSecret }) => {
self.$hideLoading();
self.$user.unlockTokenByWebAuthn(id, userName, userSecret);
unlockTokenByWebAuthn(id, userName, userSecret);
self.transactionsStore.initTransactionDraft();
self.tokensStore.refreshTokenAndRevokeOldToken().then(response => {
if (response.user) {
@@ -189,7 +195,7 @@ export default {
}
try {
self.$user.unlockTokenByPinCode(user.username, pinCode);
unlockTokenByPinCode(user.username, pinCode);
self.transactionsStore.initTransactionDraft();
self.tokensStore.refreshTokenAndRevokeOldToken().then(response => {
if (response.user) {