From f8da5f455a15f27e93e8b317af3bf9dba7dc3d57 Mon Sep 17 00:00:00 2001 From: Vinzent Date: Thu, 15 Sep 2022 22:11:47 +0200 Subject: [PATCH] fix: retry auth with different credentials fix #296 --- src/isomorphicGit.ts | 21 ++++++++++++++++++++- src/main.ts | 5 +++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/isomorphicGit.ts b/src/isomorphicGit.ts index 54a6dc4a..0a21fd92 100644 --- a/src/isomorphicGit.ts +++ b/src/isomorphicGit.ts @@ -1,9 +1,10 @@ -import git, { AuthCallback, Errors, GitHttpRequest, GitHttpResponse, GitProgressEvent, HttpClient, Walker } from "isomorphic-git"; +import git, { AuthCallback, AuthFailureCallback, Errors, GitHttpRequest, GitHttpResponse, GitProgressEvent, HttpClient, Walker } from "isomorphic-git"; import { Notice, requestUrl } from 'obsidian'; import { GitManager } from "./gitManager"; import ObsidianGit from './main'; import { MyAdapter } from './myAdapter'; import { BranchInfo, FileStatusResult, PluginState, Status, UnstagedFile, WalkDifference } from "./types"; +import { GeneralModal } from "./ui/modals/generalModal"; import { worthWalking } from "./utils"; @@ -41,6 +42,7 @@ export class IsomorphicGit extends GitManager { fs: MyAdapter, dir: string, onAuth: AuthCallback, + onAuthFailure: AuthFailureCallback, http: HttpClient, } { return { @@ -52,6 +54,23 @@ export class IsomorphicGit extends GitManager { password: this.plugin.localStorage.getPassword() }; }, + onAuthFailure: async () => { + new Notice("Authentication failed. Please try with different credentials"); + const username = await new GeneralModal(app, [], "Specify your username").open(); + if (username) { + const password = await new GeneralModal(app, [], "Specify your password/personal access token").open(); + if (password) { + this.plugin.settings.username = username; + await this.plugin.saveSettings(); + this.plugin.localStorage.setPassword(password); + return { + username, + password + }; + } + } + return { cancel: true }; + }, http: { async request({ url, diff --git a/src/main.ts b/src/main.ts index fd59c6fb..fcc62d18 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,3 +1,4 @@ +import { Errors } from "isomorphic-git"; import { debounce, Debouncer, EventRef, Menu, normalizePath, Notice, Platform, Plugin, TAbstractFile, TFile } from "obsidian"; import { PromiseQueue } from "src/promiseQueue"; import { ObsidianGitSettingsTab } from "src/settings"; @@ -1045,6 +1046,10 @@ export default class ObsidianGit extends Plugin { console.log(`git obsidian message: ${message}`); } displayError(message: any, timeout: number = 10 * 1000): void { + if (message instanceof Errors.UserCanceledError) { + new Notice("Aborted"); + return; + } // Some errors might not be of type string message = message.toString(); new Notice(message, timeout);