diff --git a/src/stores/auth.js b/src/stores/auth.js index 47b19951..15f6e0a7 100644 --- a/src/stores/auth.js +++ b/src/stores/auth.js @@ -15,7 +15,6 @@ import { useAdvancedSettingsStore } from './settings/advanced'; import { useNotificationStore } from './notification'; import { useUpdateLoopStore } from './updateLoop'; import { useUserStore } from './user'; -import { useVrcxStore } from './vrcx'; import { watchState } from '../service/watchState'; import configRepository from '../service/config'; @@ -27,7 +26,6 @@ export const useAuthStore = defineStore('Auth', () => { const notificationStore = useNotificationStore(); const userStore = useUserStore(); const updateLoopStore = useUpdateLoopStore(); - const vrcxStore = useVrcxStore(); const { t } = useI18n(); const state = reactive({ @@ -43,7 +41,6 @@ export const useAuthStore = defineStore('Auth', () => { endpoint: '', websocket: '', saveCredentials: false, - savedCredentials: {}, lastUserLoggedIn: '', rules: { username: [ @@ -144,16 +141,29 @@ export const useAuthStore = defineStore('Auth', () => { JSON.stringify(credentials) ); } - loginForm.value.savedCredentials = credentials; } catch (error) { console.error('Failed to parse savedCredentials:', error); - loginForm.value.savedCredentials = {}; } state.enableCustomEndpoint = enableCustomEndpoint; } init(); + async function getSavedCredentials(userId) { + let savedCredentials = {}; + try { + savedCredentials = JSON.parse( + await configRepository.getString('savedCredentials', '{}') + ); + } catch (e) { + console.error('Failed to get saved credentials:', e); + } + if (!savedCredentials) { + return null; + } + return savedCredentials[userId]; + } + async function handleLogoutEvent() { if (watchState.isLoggedIn) { new Noty({ @@ -187,10 +197,9 @@ export const useAuthStore = defineStore('Auth', () => { !advancedSettingsStore.enablePrimaryPassword && (await configRepository.getString('lastUserLoggedIn')) !== null ) { - const user = - loginForm.value.savedCredentials[ - loginForm.value.lastUserLoggedIn - ]; + const user = await getSavedCredentials( + loginForm.value.lastUserLoggedIn + ); if (user?.loginParams?.endpoint) { AppDebug.endpointDomain = user.loginParams.endpoint; AppDebug.websocketDomain = user.loginParams.websocket; @@ -220,11 +229,10 @@ export const useAuthStore = defineStore('Auth', () => { async function clearCookiesTryLogin() { await webApiService.clearCookies(); if (loginForm.value.lastUserLoggedIn) { - const user = - loginForm.value.savedCredentials[ - loginForm.value.lastUserLoggedIn - ]; - if (typeof user !== 'undefined') { + const user = await getSavedCredentials( + loginForm.value.lastUserLoggedIn + ); + if (user) { delete user.cookies; await relogin(user); } @@ -233,11 +241,10 @@ export const useAuthStore = defineStore('Auth', () => { async function resendEmail2fa() { if (loginForm.value.lastUserLoggedIn) { - const user = - loginForm.value.savedCredentials[ - loginForm.value.lastUserLoggedIn - ]; - if (typeof user !== 'undefined') { + const user = await getSavedCredentials( + loginForm.value.lastUserLoggedIn + ); + if (user) { await webApiService.clearCookies(); delete user.cookies; relogin(user).then(() => { @@ -272,24 +279,28 @@ export const useAuthStore = defineStore('Auth', () => { inputPattern: /[\s\S]{1,32}/ } ) - .then(({ value }) => { - for (const userId in loginForm.value.savedCredentials) { + .then(async ({ value }) => { + let savedCredentials = JSON.parse( + await configRepository.getString( + 'savedCredentials', + '{}' + ) + ); + for (const userId in savedCredentials) { security .decrypt( - loginForm.value.savedCredentials[userId] - .loginParams.password, + savedCredentials[userId].loginParams.password, value ) .then(async (pt) => { credentialsToSave.value = { username: - loginForm.value.savedCredentials[userId] - .loginParams.username, + savedCredentials[userId].loginParams + .username, password: pt }; await updateStoredUser( - loginForm.value.savedCredentials[userId] - .user + savedCredentials[userId].user ); await configRepository.setBool( 'enablePrimaryPassword', @@ -320,23 +331,19 @@ export const useAuthStore = defineStore('Auth', () => { enablePrimaryPasswordDialog.value.visible = false; if (advancedSettingsStore.enablePrimaryPassword) { const key = enablePrimaryPasswordDialog.value.password; - for (const userId in loginForm.value.savedCredentials) { + let savedCredentials = JSON.parse( + await configRepository.getString('savedCredentials', '{}') + ); + for (const userId in savedCredentials) { security - .encrypt( - loginForm.value.savedCredentials[userId].loginParams - .password, - key - ) + .encrypt(savedCredentials[userId].loginParams.password, key) .then((ct) => { credentialsToSave.value = { username: - loginForm.value.savedCredentials[userId] - .loginParams.username, + savedCredentials[userId].loginParams.username, password: ct }; - updateStoredUser( - loginForm.value.savedCredentials[userId].user - ); + updateStoredUser(savedCredentials[userId].user); }); } } @@ -366,7 +373,6 @@ export const useAuthStore = defineStore('Auth', () => { savedCredentials[user.id].cookies = await webApiService.getCookies(); } - loginForm.value.savedCredentials = savedCredentials; const jsonCredentialsArray = JSON.stringify(savedCredentials); await configRepository.setString( 'savedCredentials', @@ -531,9 +537,10 @@ export const useAuthStore = defineStore('Auth', () => { false ); } - loginForm.value.savedCredentials = savedCredentials; - const jsonCredentials = JSON.stringify(savedCredentials); - await configRepository.setString('savedCredentials', jsonCredentials); + await configRepository.setString( + 'savedCredentials', + JSON.stringify(savedCredentials) + ); new Noty({ type: 'success', text: 'Account removed.' @@ -571,12 +578,15 @@ export const useAuthStore = defineStore('Auth', () => { inputPattern: /[\s\S]{1,32}/ } ) - .then(({ value }) => { + .then(async ({ value }) => { + const savedCredentials = JSON.parse( + await configRepository.getString( + 'savedCredentials' + ) + ); const saveCredential = - loginForm.value.savedCredentials[ - Object.keys( - loginForm.value.savedCredentials - )[0] + savedCredentials[ + Object.keys(savedCredentials)[0] ]; security .decrypt( @@ -823,9 +833,8 @@ export const useAuthStore = defineStore('Auth', () => { return; } attemptingAutoLogin.value = true; - const user = - loginForm.value.savedCredentials[loginForm.value.lastUserLoggedIn]; - if (typeof user === 'undefined') { + const user = getSavedCredentials(loginForm.value.lastUserLoggedIn); + if (!user) { attemptingAutoLogin.value = false; return; } diff --git a/src/views/Login/Login.vue b/src/views/Login/Login.vue index 646b73ea..f17c62c7 100644 --- a/src/views/Login/Login.vue +++ b/src/views/Login/Login.vue @@ -92,19 +92,19 @@ > -