-
Notifications
You must be signed in to change notification settings - Fork 4
[ENG-1177] Authenticate Obsidian user for publishing #656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
trangdoan982
merged 5 commits into
main
from
eng-1177-authenticate-obsidian-against-dg-space
Jan 8, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| import type { Enums } from "@repo/database/dbTypes"; | ||
| import type { DGSupabaseClient } from "@repo/database/lib/client"; | ||
| import { | ||
| fetchOrCreateSpaceDirect, | ||
| fetchOrCreatePlatformAccount, | ||
| createLoggedInClient, | ||
| } from "@repo/database/lib/contextFunctions"; | ||
| import type DiscourseGraphPlugin from "~/index"; | ||
|
|
||
| type Platform = Enums<"Platform">; | ||
|
|
||
| export type SupabaseContext = { | ||
| platform: Platform; | ||
| spaceId: number; | ||
| userId: number; | ||
| spacePassword: string; | ||
| }; | ||
|
|
||
| let contextCache: SupabaseContext | null = null; | ||
|
|
||
| const generateAccountLocalId = (vaultName: string): string => { | ||
| const randomSuffix = Math.random().toString(36).substring(2, 8).toUpperCase(); | ||
| const sanitizedVaultName = vaultName | ||
| .replace(/\s+/g, "") | ||
| .replace(/[^a-zA-Z0-9]/g, "") | ||
| .replace(/-+/g, "-"); | ||
| return `${sanitizedVaultName}${randomSuffix}`; | ||
| }; | ||
|
|
||
| const getOrCreateSpacePassword = async ( | ||
| plugin: DiscourseGraphPlugin, | ||
| ): Promise<string> => { | ||
| if (plugin.settings.spacePassword) { | ||
| return plugin.settings.spacePassword; | ||
| } | ||
| const password = crypto.randomUUID(); | ||
| plugin.settings.spacePassword = password; | ||
| await plugin.saveSettings(); | ||
| return password; | ||
| }; | ||
|
|
||
| const getOrCreateAccountLocalId = async ( | ||
| plugin: DiscourseGraphPlugin, | ||
| vaultName: string, | ||
| ): Promise<string> => { | ||
| if (plugin.settings.accountLocalId) { | ||
| return plugin.settings.accountLocalId; | ||
| } | ||
| const accountLocalId = generateAccountLocalId(vaultName); | ||
| plugin.settings.accountLocalId = accountLocalId; | ||
| await plugin.saveSettings(); | ||
| return accountLocalId; | ||
| }; | ||
|
|
||
| /** | ||
| * Gets the unique vault ID from Obsidian's internal API. | ||
| * @see https://help.obsidian.md/Extending+Obsidian/Obsidian+URI | ||
| */ | ||
| const getVaultId = (app: DiscourseGraphPlugin["app"]): string => { | ||
| return (app as unknown as { appId: string }).appId; | ||
| }; | ||
|
|
||
| const canonicalObsidianUrl = (vaultId: string): string => { | ||
| return `obsidian:${vaultId}`; | ||
| }; | ||
|
|
||
| export const getSupabaseContext = async ( | ||
| plugin: DiscourseGraphPlugin, | ||
| ): Promise<SupabaseContext | null> => { | ||
| if (contextCache === null) { | ||
| try { | ||
| const vaultName = plugin.app.vault.getName() || "obsidian-vault"; | ||
| const vaultId = getVaultId(plugin.app); | ||
|
|
||
| const spacePassword = await getOrCreateSpacePassword(plugin); | ||
| const accountLocalId = await getOrCreateAccountLocalId(plugin, vaultName); | ||
|
|
||
| const url = canonicalObsidianUrl(vaultId); | ||
| const platform: Platform = "Obsidian"; | ||
|
|
||
| const spaceResult = await fetchOrCreateSpaceDirect({ | ||
| password: spacePassword, | ||
| url, | ||
| name: vaultName, | ||
| platform, | ||
| }); | ||
|
|
||
| if (!spaceResult.data) { | ||
| console.error("Failed to create space"); | ||
| return null; | ||
| } | ||
|
|
||
| const spaceId = spaceResult.data.id; | ||
| const userId = await fetchOrCreatePlatformAccount({ | ||
trangdoan982 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| platform: "Obsidian", | ||
| accountLocalId, | ||
| name: vaultName, | ||
| email: accountLocalId, | ||
| spaceId, | ||
| password: spacePassword, | ||
| }); | ||
|
|
||
| contextCache = { | ||
| platform: "Obsidian", | ||
| spaceId, | ||
| userId, | ||
| spacePassword, | ||
| }; | ||
| } catch (error) { | ||
| console.error(error); | ||
| return null; | ||
| } | ||
| } | ||
| return contextCache; | ||
| }; | ||
|
|
||
| let loggedInClient: DGSupabaseClient | null = null; | ||
|
|
||
| export const getLoggedInClient = async ( | ||
| plugin: DiscourseGraphPlugin, | ||
| ): Promise<DGSupabaseClient | null> => { | ||
| if (loggedInClient === null) { | ||
| const context = await getSupabaseContext(plugin); | ||
| if (context === null) { | ||
| throw new Error("Could not create Supabase context"); | ||
| } | ||
| try { | ||
| loggedInClient = await createLoggedInClient({ | ||
| platform: context.platform, | ||
| spaceId: context.spaceId, | ||
| password: context.spacePassword, | ||
| }); | ||
| if (!loggedInClient) { | ||
| throw new Error( | ||
| "Failed to create Supabase client - check environment variables", | ||
| ); | ||
| } | ||
| } catch (error) { | ||
| const errorMessage = | ||
| error instanceof Error ? error.message : String(error); | ||
| console.error("Failed to create logged-in client:", errorMessage); | ||
| throw new Error(`Supabase authentication failed: ${errorMessage}`); | ||
| } | ||
| } else { | ||
| // renew session | ||
| const { error } = await loggedInClient.auth.getSession(); | ||
| if (error) { | ||
| console.warn("Session renewal failed, re-authenticating:", error); | ||
| loggedInClient = null; | ||
| const context = await getSupabaseContext(plugin); | ||
| if (context === null) { | ||
| throw new Error( | ||
| "Could not create Supabase context for re-authentication", | ||
| ); | ||
| } | ||
|
|
||
| loggedInClient = await createLoggedInClient({ | ||
| platform: context.platform, | ||
| spaceId: context.spaceId, | ||
| password: context.spacePassword, | ||
| }); | ||
| if (!loggedInClient) { | ||
| throw new Error("Failed to re-authenticate Supabase client"); | ||
| } | ||
| } | ||
| } | ||
| return loggedInClient; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { getSupabaseContext } from "./supabaseContext"; | ||
| import type DiscourseGraphPlugin from "~/index"; | ||
|
|
||
| export const initializeSupabaseSync = async ( | ||
| plugin: DiscourseGraphPlugin, | ||
| ): Promise<void> => { | ||
| const context = await getSupabaseContext(plugin); | ||
| if (!context) { | ||
| throw new Error("Failed to initialize Supabase sync: could not create context"); | ||
| } | ||
| console.log("Supabase sync initialized successfully", { | ||
| spaceId: context.spaceId, | ||
| userId: context.userId, | ||
| }); | ||
| }; | ||
trangdoan982 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.