@@ -19,11 +19,12 @@ export type SupabaseContext = {
1919let contextCache : SupabaseContext | null = null ;
2020
2121const 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 - z A - Z 0 - 9 ] / g, "" )
26+ . replace ( / - + / g, "-" ) ;
27+ return `${ sanitizedVaultName } ${ randomSuffix } ` ;
2728} ;
2829
2930const 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 ;
0 commit comments