@@ -47,6 +47,7 @@ export interface ModuleOptions {
4747 * @docs https://supabase.com/blog/jwt-signing-keys
4848 */
4949 secretKey : string
50+
5051 /**
5152 * Redirect automatically to login page if user is not authenticated
5253 * @default `true`
@@ -181,11 +182,27 @@ export default defineNuxtModule<ModuleOptions>({
181182 logger . warn ( 'Missing supabase url, set it either in `nuxt.config.ts` or via env variable' )
182183 }
183184 else {
184- // Use the default storage key as defined by the supabase-js client if no cookiePrefix is set.
185- // Source: https://github.com/supabase/supabase-js/blob/3316f2426d7c2e5babaab7ddc17c30bfa189f500/src/SupabaseClient.ts#L86
186- const defaultStorageKey = `sb-${ new URL ( finalUrl ) . hostname . split ( '.' ) [ 0 ] } -auth-token`
187- const currentPrefix = nuxt . options . runtimeConfig . public . supabase . cookiePrefix
188- nuxt . options . runtimeConfig . public . supabase . cookiePrefix = currentPrefix || defaultStorageKey
185+ try {
186+ // Use the default storage key as defined by the supabase-js client if no cookiePrefix is set.
187+ // Source: https://github.com/supabase/supabase-js/blob/3316f2426d7c2e5babaab7ddc17c30bfa189f500/src/SupabaseClient.ts#L86
188+ const defaultStorageKey = `sb-${ new URL ( finalUrl ) . hostname . split ( '.' ) [ 0 ] } -auth-token`
189+ const currentPrefix = nuxt . options . runtimeConfig . public . supabase . cookiePrefix
190+ nuxt . options . runtimeConfig . public . supabase . cookiePrefix = currentPrefix || defaultStorageKey
191+ }
192+ catch ( error ) {
193+ logger . error (
194+ `Invalid Supabase URL: "${ finalUrl } ". `
195+ + `Please provide a valid URL (e.g., https://example.supabase.co or http://localhost:5432)` , error )
196+
197+ // Use fallback prefix
198+ const currentPrefix = nuxt . options . runtimeConfig . public . supabase . cookiePrefix
199+ nuxt . options . runtimeConfig . public . supabase . cookiePrefix = currentPrefix || 'sb-auth-token'
200+
201+ // Fail build in production
202+ if ( ! nuxt . options . dev ) {
203+ throw new Error ( 'Invalid Supabase URL configuration' )
204+ }
205+ }
189206 }
190207
191208 // Warn if the key isn't set.
@@ -266,13 +283,24 @@ export default defineNuxtModule<ModuleOptions>({
266283 filename : 'types/supabase-database.d.ts' ,
267284 getContents : async ( ) => {
268285 if ( options . types ) {
269- // resolvePath is used to minify user input error.
270- const path = await resolvePath ( options . types )
271- const typesPath = await resolvePath ( '~~/.nuxt/types/' ) // this is the default path for nuxt types
272-
273- if ( fs . existsSync ( path ) ) {
274- // Make the path relative to the "types" directory.
275- return `export * from '${ relative ( typesPath , path ) } '`
286+ try {
287+ // resolvePath is used to minify user input error.
288+ const path = await resolvePath ( options . types )
289+ const typesPath = await resolvePath ( '~~/.nuxt/types/' ) // this is the default path for nuxt types
290+
291+ if ( fs . existsSync ( path ) ) {
292+ // Make the path relative to the "types" directory.
293+ return `export * from '${ relative ( typesPath , path ) } '`
294+ }
295+ else {
296+ logger . warn (
297+ `Database types configured at "${ options . types } " but file not found at "${ path } ". `
298+ + `Using "Database = unknown".` ,
299+ )
300+ }
301+ }
302+ catch ( error ) {
303+ logger . error ( `Failed to load Supabase database types from "${ options . types } ":` , error )
276304 }
277305 }
278306
0 commit comments