Skip to content

Commit 3f40d5c

Browse files
committed
redo the auth so that only one user acc is created
1 parent 8696de3 commit 3f40d5c

File tree

4 files changed

+65
-27
lines changed

4 files changed

+65
-27
lines changed

apps/obsidian/src/utils/supabaseContext.ts

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ export type SupabaseContext = {
1919
let contextCache: SupabaseContext | null = null;
2020

2121
const generateAccountLocalId = (vaultName: string): string => {
22-
const randomSuffix = Math.random()
23-
.toString(36)
24-
.substring(2, 8)
25-
.toUpperCase();
26-
return `${vaultName}-${randomSuffix}`;
22+
const randomSuffix = Math.random().toString(36).substring(2, 8).toUpperCase();
23+
const sanitizedVaultName = vaultName
24+
.replace(/\s+/g, "")
25+
.replace(/[^a-zA-Z0-9]/g, "")
26+
.replace(/-+/g, "-");
27+
return `${sanitizedVaultName}${randomSuffix}`;
2728
};
2829

2930
const getOrCreateSpacePassword = async (
@@ -32,7 +33,6 @@ const getOrCreateSpacePassword = async (
3233
if (plugin.settings.spacePassword) {
3334
return plugin.settings.spacePassword;
3435
}
35-
// Generate UUID using crypto.randomUUID()
3636
const password = crypto.randomUUID();
3737
plugin.settings.spacePassword = password;
3838
await plugin.saveSettings();
@@ -62,7 +62,6 @@ export const getSupabaseContext = async (
6262
const spacePassword = await getOrCreateSpacePassword(plugin);
6363
const accountLocalId = await getOrCreateAccountLocalId(plugin, vaultName);
6464

65-
// Space URL format: "space" + accountLocalId
6665
const url = `space${accountLocalId}`;
6766
const platform: Platform = "Obsidian";
6867

@@ -83,7 +82,7 @@ export const getSupabaseContext = async (
8382
platform: "Obsidian",
8483
accountLocalId,
8584
name: vaultName,
86-
email: undefined,
85+
email: accountLocalId,
8786
spaceId,
8887
password: spacePassword,
8988
});
@@ -109,17 +108,47 @@ export const getLoggedInClient = async (
109108
): Promise<DGSupabaseClient | null> => {
110109
if (loggedInClient === null) {
111110
const context = await getSupabaseContext(plugin);
112-
if (context === null) throw new Error("Could not create context");
113-
loggedInClient = await createLoggedInClient(
114-
context.platform,
115-
context.spaceId,
116-
context.spacePassword,
117-
);
111+
if (context === null) {
112+
throw new Error("Could not create Supabase context");
113+
}
114+
try {
115+
loggedInClient = await createLoggedInClient({
116+
platform: context.platform,
117+
spaceId: context.spaceId,
118+
password: context.spacePassword,
119+
});
120+
if (!loggedInClient) {
121+
throw new Error(
122+
"Failed to create Supabase client - check environment variables",
123+
);
124+
}
125+
} catch (error) {
126+
const errorMessage =
127+
error instanceof Error ? error.message : String(error);
128+
console.error("Failed to create logged-in client:", errorMessage);
129+
throw new Error(`Supabase authentication failed: ${errorMessage}`);
130+
}
118131
} else {
119132
// renew session
120133
const { error } = await loggedInClient.auth.getSession();
121134
if (error) {
135+
console.warn("Session renewal failed, re-authenticating:", error);
122136
loggedInClient = null;
137+
const context = await getSupabaseContext(plugin);
138+
if (context === null) {
139+
throw new Error(
140+
"Could not create Supabase context for re-authentication",
141+
);
142+
}
143+
144+
loggedInClient = await createLoggedInClient({
145+
platform: context.platform,
146+
spaceId: context.spaceId,
147+
password: context.spacePassword,
148+
});
149+
if (!loggedInClient) {
150+
throw new Error("Failed to re-authenticate Supabase client");
151+
}
123152
}
124153
}
125154
return loggedInClient;

apps/roam/src/utils/supabaseContext.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ export const getLoggedInClient = async (): Promise<DGSupabaseClient | null> => {
9696
if (_loggedInClient === null) {
9797
const context = await getSupabaseContext();
9898
if (context === null) throw new Error("Could not create context");
99-
_loggedInClient = await createLoggedInClient(
100-
context.platform,
101-
context.spaceId,
102-
context.spacePassword,
103-
);
99+
_loggedInClient = await createLoggedInClient({
100+
platform: context.platform,
101+
spaceId: context.spaceId,
102+
password: context.spacePassword,
103+
});
104104
} else {
105105
// renew session
106106
const { error } = await _loggedInClient.auth.getSession();

packages/database/src/lib/contextFunctions.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,20 @@ export const fetchOrCreateSpaceDirect = async (
123123
};
124124
};
125125

126-
export const createLoggedInClient = async (
127-
platform: Platform,
128-
spaceId: number,
129-
password: string,
130-
): Promise<DGSupabaseClient | null> => {
126+
export const createLoggedInClient = async ({
127+
platform,
128+
spaceId,
129+
password,
130+
}: {
131+
platform: Platform;
132+
spaceId: number;
133+
password: string;
134+
}): Promise<DGSupabaseClient | null> => {
131135
const loggedInClient: DGSupabaseClient | null = createClient();
132136
if (!loggedInClient) return null;
137+
const email = spaceAnonUserEmail(platform, spaceId);
133138
const { error } = await loggedInClient.auth.signInWithPassword({
134-
email: spaceAnonUserEmail(platform, spaceId),
139+
email,
135140
password: password,
136141
});
137142
if (error) {
@@ -155,7 +160,11 @@ export const fetchOrCreatePlatformAccount = async ({
155160
spaceId: number;
156161
password: string;
157162
}): Promise<number> => {
158-
const supabase = await createLoggedInClient(platform, spaceId, password);
163+
const supabase = await createLoggedInClient({
164+
platform,
165+
spaceId,
166+
password,
167+
});
159168
if (!supabase) throw Error("Missing database connection");
160169

161170
const result = await supabase.rpc("create_account_in_space", {

packages/database/supabase/functions/create-space/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,4 +260,4 @@ Deno.serve(async (req) => {
260260
--header 'Content-Type: application/json' \
261261
--data '{ "url":"https://roamresearch.com/#/app/abc", "name":"abc","platform":"Roam", "password": "abcdefgh" }'
262262
263-
*/
263+
*/

0 commit comments

Comments
 (0)