Skip to content

Commit

Permalink
🚸 (Auth) Ensure user stays logged in to Swiftink
Browse files Browse the repository at this point in the history
Avoids excessive prompting to user sign in by storing session in settings
  • Loading branch information
djmango committed Oct 10, 2023
1 parent 764402c commit f5af07f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
47 changes: 32 additions & 15 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}

Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -32,6 +34,8 @@ const DEFAULT_SETTINGS: TranscriptionSettings = {
embedSummary: true,
embedOutline: true,
embedKeywords: true,
swiftink_access_token: null,
swiftink_refresh_token: null,
};

const LANGUAGES = {
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit f5af07f

Please sign in to comment.