supabase / supabase-js

An isomorphic Javascript client for Supabase. Query your Supabase database, subscribe to realtime events, upload and download files, browse typescript examples, invoke postgres functions via rpc, invoke supabase edge functions, query pgvector.
https://supabase.com
MIT License
3.11k stars 247 forks source link

Can't insantiate Supabase client; fails with GoTrue error "window is not defined." #1249

Open Stokestack opened 1 month ago

Stokestack commented 1 month ago

Bug report

There is a similar-sounding and possibly related bug here: https://github.com/supabase/supabase-js/issues/786 But the workaround appears to be React-specific and doesn't seem to address the underlying problem.

Describe the bug

I wrote a Deno-based server that is attempting to use Supabase. Upon attempting to insantiate the Supabase client, the application stops with an assertion thrown from GoTrueClient.js, _isImplicitGrantFlow():

In GoTrueClient.js, _isImplicitGrantFlow()

Exception has occurred: ReferenceError: window is not defined
  at SupabaseAuthClient._isImplicitGrantFlow (file:///Users/stokestack/Library/Caches/deno/npm/registry.npmjs.org/@supabase/auth-js/2.64.2/dist/main/GoTrueClient.js:1138:62)
    at SupabaseAuthClient._initialize (file:///Users/stokestack/Library/Caches/deno/npm/registry.npmjs.org/@supabase/auth-js/2.64.2/dist/main/GoTrueClient.js:170:64)
    at file:///Users/stokestack/Library/Caches/deno/npm/registry.npmjs.org/@supabase/auth-js/2.64.2/dist/main/GoTrueClient.js:155:35
    at file:///Users/stokestack/Library/Caches/deno/npm/registry.npmjs.org/@supabase/auth-js/2.64.2/dist/main/GoTrueClient.js:749:36
    at SupabaseAuthClient.lockNoOp [as lock] (file:///Users/stokestack/Library/Caches/deno/npm/registry.npmjs.org/@supabase/auth-js/2.64.2/dist/main/GoTrueClient.js:33:18)
    at SupabaseAuthClient._acquireLock (file:///Users/stokestack/Library/Caches/deno/npm/registry.npmjs.org/@supabase/auth-js/2.64.2/dist/main/GoTrueClient.js:745:31)
    at file:///Users/stokestack/Library/Caches/deno/npm/registry.npmjs.org/@supabase/auth-js/2.64.2/dist/main/GoTrueClient.js:154:31
    at SupabaseAuthClient.initialize (file:///Users/stokestack/Library/Caches/deno/npm/registry.npmjs.org/@supabase/auth-js/2.64.2/dist/main/GoTrueClient.js:157:11)
    at new GoTrueClient (file:///Users/stokestack/Library/Caches/deno/npm/registry.npmjs.org/@supabase/auth-js/2.64.2/dist/main/GoTrueClient.js:136:14)
    at new SupabaseAuthClient (https://jsr.io/@supabase/supabase-js/2.44.3/src/lib/SupabaseAuthClient.ts:6:5)

To Reproduce

Set up a local-development environment for Supabase, and populate its public DB schema with a few tables. Run the Supabase TypeScript type generation on it and direct the output to DBTypes.ts in the source directory struture of any Deno project.

In that Deno project, try this:

import { createClient } from 'jsr:@supabase/supabase-js@2';
import { Database, Tables, Enums, TablesInsert, TablesUpdate } from './DBTypes.ts';

export const supabase = createClient<Database>('http://localhost:54321', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0')

Expected behavior

The client is created and I can start executing queries with it.

Screenshots

Screenshot 2024-07-20 at 2 34 39 PM

System information

Additional context

Obviously this is a show-stopper.

j4w8n commented 1 month ago

Looks like this got resolved on Discord:

I'm not familiar with how you're using Deno, but if you add this option to your client, you shouldn't see the error. It's implying that you aren't doing implicit auth (and maybe you don't need auth anyway), and therefore won't call the _isImplictGrantFlow code.

const supabase = createClient(url, key, {
  auth: {
    detectSessionInUrl: false
  }
})
Stokestack commented 1 month ago

Thanks. I wouldn't say it's resolved, though, because this outcome isn't documented (as far as I've seen). If you invoke the client as documented, it fails.