From f5af07f52078ed3c5f078768e5dbcd263aa926b6 Mon Sep 17 00:00:00 2001 From: Sulaiman Ghori Date: Tue, 10 Oct 2023 14:20:26 -0400 Subject: [PATCH] :children_crossing: (Auth) Ensure user stays logged in to Swiftink Avoids excessive prompting to user sign in by storing session in settings --- src/main.ts | 47 ++++++++++++++++++++++++++++++++--------------- src/settings.ts | 7 +++++++ 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/main.ts b/src/main.ts index 92073c1..09debac 100644 --- a/src/main.ts +++ b/src/main.ts @@ -73,25 +73,37 @@ export default class Transcription extends Plugin { ); // Prompt the user to sign in if the have Swiftink selected and are not signed in - if ( - this.settings.transcription_engine == "swiftink" && - (await this.supabase.auth.getSession().then((res) => { - return res.data.session == null; - })) - ) { - new Notice( - "Transcription: Please sign in to Swiftink.io via the settings tab", - 4000, - ); - } else if (this.settings.transcription_engine == "swiftink") { + if (this.settings.transcription_engine == "swiftink") { this.user = await this.supabase.auth.getUser().then((res) => { return res.data.user || null; }); if (this.user == null) { - new Notice( - "Transcription: Please sign in to Swiftink.io via the settings tab", - 4000, - ); + // First try setting the access token and refresh token from the settings + if (this.settings.debug) + console.log( + "Trying to set access token and refresh token from settings", + ); + if ( + this.settings.swiftink_access_token != null && + this.settings.swiftink_refresh_token != null + ) { + await this.supabase.auth.setSession({ + access_token: this.settings.swiftink_access_token, + refresh_token: this.settings.swiftink_refresh_token, + }); + this.user = await this.supabase.auth + .getUser() + .then((res) => { + return res.data.user || null; + }); + } + + // If the user is still null, prompt them to sign in + if (this.user == null) + new Notice( + "Transcription: Please sign in to Swiftink.io via the settings tab", + 4000, + ); } } @@ -291,6 +303,11 @@ export default class Transcription extends Plugin { }); new Notice("Successfully authenticated with Swiftink.io"); + // Save to settings + this.settings.swiftink_access_token = access_token; + this.settings.swiftink_refresh_token = refresh_token; + await this.saveSettings(); + // Show the settings for user auth/unauth based on whether the user is signed in if (this.user == null) { document diff --git a/src/settings.ts b/src/settings.ts index b61ccf7..f1afd44 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -14,6 +14,8 @@ interface TranscriptionSettings { embedSummary: boolean; embedOutline: boolean; embedKeywords: boolean; + swiftink_access_token: string | null; + swiftink_refresh_token: string | null; } const SWIFTINK_AUTH_CALLBACK = @@ -32,6 +34,8 @@ const DEFAULT_SETTINGS: TranscriptionSettings = { embedSummary: true, embedOutline: true, embedKeywords: true, + swiftink_access_token: null, + swiftink_refresh_token: null, }; const LANGUAGES = { @@ -263,6 +267,9 @@ class TranscriptionSettingTab extends PluginSettingTab { bt.onClick(async () => { await this.plugin.supabase.auth.signOut(); this.plugin.user = null; + this.plugin.settings.swiftink_access_token = null; + this.plugin.settings.swiftink_refresh_token = null; + await this.plugin.saveSettings(); containerEl .findAll(".swiftink-unauthed-only") .forEach((element) => {