-
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
Open
trangdoan982
wants to merge
4
commits into
main
Choose a base branch
from
eng-1177-authenticate-obsidian-against-dg-space
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,126 @@ | ||
| 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(); | ||
| return `${vaultName}-${randomSuffix}`; | ||
| }; | ||
|
|
||
| const getOrCreateSpacePassword = async ( | ||
| plugin: DiscourseGraphPlugin, | ||
| ): Promise<string> => { | ||
| if (plugin.settings.spacePassword) { | ||
| return plugin.settings.spacePassword; | ||
| } | ||
| // Generate UUID using crypto.randomUUID() | ||
| 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; | ||
| }; | ||
|
|
||
| export const getSupabaseContext = async ( | ||
| plugin: DiscourseGraphPlugin, | ||
| ): Promise<SupabaseContext | null> => { | ||
| if (contextCache === null) { | ||
| try { | ||
| const vaultName = plugin.app.vault.getName() || "obsidian-vault"; | ||
|
|
||
| const spacePassword = await getOrCreateSpacePassword(plugin); | ||
| const accountLocalId = await getOrCreateAccountLocalId(plugin, vaultName); | ||
|
|
||
| // Space URL format: "space" + accountLocalId | ||
| const url = `space${accountLocalId}`; | ||
| 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({ | ||
| platform: "Obsidian", | ||
| accountLocalId, | ||
| name: vaultName, | ||
| email: undefined, | ||
| 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 context"); | ||
| loggedInClient = await createLoggedInClient( | ||
| context.platform, | ||
| context.spaceId, | ||
| context.spacePassword, | ||
| ); | ||
| } else { | ||
| // renew session | ||
| const { error } = await loggedInClient.auth.getSession(); | ||
| if (error) { | ||
| loggedInClient = null; | ||
| } | ||
| } | ||
| 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm following Roam pattern here to create both Anonymous User and the real user (name here is vaultName instead of "Anonymous of space [x]". But I would say we don't need this for Obsidian since each vault is a space, and is owned by only one person? hence all the handling would go through only need to go through 1 account? I can just comment out this part of the code to delete this real user creation