From f3668ac23f13d263e50b7d6b716d91150a11b6c7 Mon Sep 17 00:00:00 2001 From: Vinzent Date: Tue, 1 Nov 2022 23:25:49 +0100 Subject: [PATCH] fix: store username in localstorage --- src/constants.ts | 1 - src/isomorphicGit.ts | 5 ++--- src/localStorageSettings.ts | 8 ++++++++ src/main.ts | 5 +++++ src/settings.ts | 5 ++--- src/types.ts | 5 ++++- 6 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index b7948929..190b61d7 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -23,7 +23,6 @@ export const DEFAULT_SETTINGS: ObsidianGitSettings = { basePath: "", differentIntervalCommitAndPush: false, changedFilesInStatusBar: false, - username: "", showedMobileNotice: false, refreshSourceControlTimer: 7000, showBranchStatusBar: true diff --git a/src/isomorphicGit.ts b/src/isomorphicGit.ts index 261a0466..83b5d378 100644 --- a/src/isomorphicGit.ts +++ b/src/isomorphicGit.ts @@ -50,7 +50,7 @@ export class IsomorphicGit extends GitManager { dir: this.plugin.settings.basePath, onAuth: () => { return { - username: this.plugin.settings.username, + username: this.plugin.localStorage.getUsername() ?? undefined, password: this.plugin.localStorage.getPassword() ?? undefined }; }, @@ -60,8 +60,7 @@ export class IsomorphicGit extends GitManager { if (username) { const password = await new GeneralModal({ placeholder: "Specify your password/personal access token" }).open(); if (password) { - this.plugin.settings.username = username; - await this.plugin.saveSettings(); + this.plugin.localStorage.setUsername(username); this.plugin.localStorage.setPassword(password); return { username, diff --git a/src/localStorageSettings.ts b/src/localStorageSettings.ts index 475b9c83..35410ff3 100644 --- a/src/localStorageSettings.ts +++ b/src/localStorageSettings.ts @@ -26,6 +26,14 @@ export class LocalStorageSettings { return app.saveLocalStorage(this.prefix + "password", value); } + getUsername(): string | null { + return app.loadLocalStorage(this.prefix + "username"); + } + + setUsername(value: string): void { + return app.saveLocalStorage(this.prefix + "username", value); + } + getHostname(): string | null { return app.loadLocalStorage(this.prefix + "hostname"); } diff --git a/src/main.ts b/src/main.ts index d67fb0b7..642332f5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -465,6 +465,11 @@ export default class ObsidianGit extends Plugin { this.settings.gitPath = undefined; await this.saveSettings(); } + if (this.settings.username != undefined) { + this.localStorage.setPassword(this.settings.username); + this.settings.username = undefined; + await this.saveSettings(); + } } unloadPlugin() { diff --git a/src/settings.ts b/src/settings.ts index 54b1bc05..5703c77b 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -431,10 +431,9 @@ export class ObsidianGitSettingsTab extends PluginSettingTab { new Setting(containerEl) .setName("Username on your git server. E.g. your username on GitHub") .addText(cb => { - cb.setValue(plugin.settings.username); + cb.setValue(plugin.localStorage.getUsername() ?? ""); cb.onChange((value) => { - plugin.settings.username = value; - plugin.saveSettings(); + plugin.localStorage.setUsername(value); }); }); diff --git a/src/types.ts b/src/types.ts index 69d1bcff..ffb2f8ef 100644 --- a/src/types.ts +++ b/src/types.ts @@ -20,7 +20,10 @@ export interface ObsidianGitSettings { customMessageOnAutoBackup: boolean; autoBackupAfterFileChange: boolean; treeStructure: boolean; - username: string; + /** + * @deprecated Using `localstorage` instead + */ + username?: string; differentIntervalCommitAndPush: boolean; changedFilesInStatusBar: boolean;