Skip to content
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

Supabase client in Expo on Web is just crashing (window is not defined) #870

Closed
ThibaultJanBeyer opened this issue Oct 4, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@ThibaultJanBeyer
Copy link

Bug report

Hi, following this tutorial, I try to run supabase js client in expo and this crashes the app on web:

// ./lib/supabase.ts
import "react-native-url-polyfill/auto";
import AsyncStorage from "@react-native-async-storage/async-storage";
import { createClient } from "@supabase/supabase-js";

...

export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
  auth: {
    storage: AsyncStorage,
    autoRefreshToken: true,
    persistSession: true,
    detectSessionInUrl: false,
  },
});

gives me:

ReferenceError: window is not defined
    at http://localhost:8081/node_modules/expo-router/node/render.bundle//&platform=web&dev=true&minify=false&resolver.environment=node&transform.environment=node:141156:9
    at http://localhost:8081/node_modules/expo-router/node/render.bundle//&platform=web&dev=true&minify=false&resolver.environment=node&transform.environment=node:141130:21
    at new Promise (<anonymous>)
    at createPromise (http://localhost:8081/node_modules/expo-router/node/render.bundle//&platform=web&dev=true&minify=false&resolver.environment=node&transform.environment=node:141128:12)
    at Object.getItem (http://localhost:8081/node_modules/expo-router/node/render.bundle//&platform=web&dev=true&minify=false&resolver.environment=node&transform.environment=node:141155:14)
    at http://localhost:8081/node_modules/expo-router/node/render.bundle//&platform=web&dev=true&minify=false&resolver.environment=node&transform.environment=node:155465:33
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (http://localhost:8081/node_modules/expo-router/node/render.bundle//&platform=web&dev=true&minify=false&resolver.environment=node&transform.environment=node:2463:26)
    at _next (http://localhost:8081/node_modules/expo-router/node/render.bundle//&platform=web&dev=true&minify=false&resolver.environment=node&transform.environment=node:2482:11)
    at http://localhost:8081/node_modules/expo-router/node/render.bundle//&platform=web&dev=true&minify=false&resolver.environment=node&transform.environment=node:2487:9
http://localhost:8081/node_modules/expo-router/node/render.bundle//&platform=web&dev=true&minify=false&resolver.environment=node&transform.environment=node:141156
        return window.localStorage.getItem(key);

When running the web application on Chrome. It does work in the Android native app tho’

Thank you!

@ThibaultJanBeyer ThibaultJanBeyer added the bug Something isn't working label Oct 4, 2023
@ThibaultJanBeyer
Copy link
Author

ThibaultJanBeyer commented Oct 4, 2023

Nevermind, it seems to be a bug in the async storage not being compatible with the web. import AsyncStorage from "@react-native-async-storage/async-storage";

So adding ...(Platform.OS !== "web" ? { storage: AsyncStorage } : {}), in the createClient solves the issue:

export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
  auth: {
    ...(Platform.OS !== "web" ? { storage: AsyncStorage } : {}),
    autoRefreshToken: true,
    persistSession: true,
    detectSessionInUrl: false,
  },
});

Closed. Sorry.

@julian-hecker
Copy link

Your solution worked great for me, thanks :)

@ayush4345
Copy link

Nevermind, it seems to be a bug in the async storage not being compatible with the web. import AsyncStorage from "@react-native-async-storage/async-storage";

So adding ...(Platform.OS !== "web" ? { storage: AsyncStorage } : {}), in the createClient solves the issue:

export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
  auth: {
    ...(Platform.OS !== "web" ? { storage: AsyncStorage } : {}),
    autoRefreshToken: true,
    persistSession: true,
    detectSessionInUrl: false,
  },
});

Closed. Sorry.

it is saying the platform is not defined. what I have to do for that.

@yurihikari
Copy link

Nevermind, it seems to be a bug in the async storage not being compatible with the web. import AsyncStorage from "@react-native-async-storage/async-storage";
So adding ...(Platform.OS !== "web" ? { storage: AsyncStorage } : {}), in the createClient solves the issue:

export const supabase = createClient(supabaseUrl, supabaseAnonKey, {
  auth: {
    ...(Platform.OS !== "web" ? { storage: AsyncStorage } : {}),
    autoRefreshToken: true,
    persistSession: true,
    detectSessionInUrl: false,
  },
});

Closed. Sorry.

it is saying the platform is not defined. what I have to do for that.

A bit late, but you need to import Platform from react native :
import { Platform } from 'react-native';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants