Supabase用户返回Null + 应用程序关闭后未保存身份验证状态

huangapple go评论63阅读模式
英文:

Supabase User Returning Null + Auth State Not Saved After Closing App

问题

以下是您提供的内容的中文翻译:

我开始学习React Native,并在React中使用Supabase与使用Flutter时有很大不同。目前,我已经成功实现了电子邮件登录/注册功能,并监听身份验证更改以切换导航堆栈。我认为我的两个问题可能相关,因为在更新时用户/会话可能没有保存。

第一个问题是每次打开应用程序时都会显示登录屏幕,即使我在关闭应用程序之前已经登录。我尝试设置一些选项,包括localStorage,但没有帮助。以下是我的supabaseClient.tsx文件。

const options = {
  auth: {
    localStorage: AsyncStorage as any,
    autoRefreshToken: true,
    persistSession: true,
    detectSessionInUrl: true,
  },
};

const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY, options);

export { supabase };

第二个问题是我正在尝试做一些简单的事情,只是在HomeScreen上显示已登录用户在displayName中设置的内容,但getUser()getSession()中的用户都返回null,因此我无法获取user.id。这导致displayName为未定义,屏幕为空白。以下是我用于获取displayName的代码。

export function HomeScreen() {
  const [displayName, setDisplayName] = useState();

  useEffect(() => {
    (async () => {
      const user = (await supabase.auth.getSession()).data.session?.user;
      console.log("User: " + user);
      const { data, error } = await supabase
        .from("profiles")
        .select()
        .eq("uid", user?.id);
      if (error === null) {
        data!.map((data) => {
          const name = data.displayName;
          console.log(name);
          setDisplayName(name);
        });
      } else {
        console.log(error.message);
      }
      console.log("Name: " + displayName);
    })();
  }, [setDisplayName]);

  return (
    <View>
      <Text>{displayName}</Text>
    </View>
  );
}

请注意,代码中的一些标记(如&gt;&lt;)似乎是HTML或XML转义字符,需要根据需要进行处理。

英文:

I am starting to learn React Native and using Supabase with React is pretty different than using it with Flutter. Right now, I have gotten email sign-in/up working and listening for auth changes to switch navigation stacks. I believe my two issues are related because the user/session might not be getting saved when updated.

The first issue is that every time the app is opened, the sign-in screen is shown, even though I signed in before closing it. I tried to set som options, including localStorage but that did not help. Below is my supabaseClient.tsx file.

const options = {
  auth: {
    localStorage: AsyncStorage as any,
    autoRefreshToken: true,
    persistSession: true,
    detectSessionInUrl: true,
  },
};

const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY, options);

export { supabase };

The second issue is that I am trying something simple, just showing the displayName that the signed-in user has set on the HomeScreen, but the getUser() and the user from getSession() both return null so I cannot get the user.id. This causes the displayName to be undefined and a blank screen. Below is the code I am using to get the displayName.

export function HomeScreen() {
  const [displayName, setDisplayName] = useState();

  useEffect(() =&gt; {
    (async () =&gt; {
      const user = (await supabase.auth.getSession()).data.session?.user;
      console.log(&quot;User: &quot; + user);
      const { data, error } = await supabase
        .from(&quot;profiles&quot;)
        .select()
        .eq(&quot;uid&quot;, user?.id);
      if (error === null) {
        data!.map((data) =&gt; {
          const name = data.displayName;
          console.log(name);
          setDisplayName(name);
        });
      } else {
        console.log(error.message);
      }
      console.log(&quot;Name: &quot; + displayName);
    })();
  }, [setDisplayName]);

  return (
    &lt;View&gt;
      &lt;Text&gt;{displayName}&lt;/Text&gt;
    &lt;/View&gt;
  );
}

答案1

得分: 0

我之前因为看到的一个教程将localStorage定义为any类型。

我需要将这个部分进行修改:

const options = {
  auth: {
    localStorage: AsyncStorage as any,
    autoRefreshToken: true,
    persistSession: true,
    detectSessionInUrl: true,
  },
};

改成这样:

const options = {
  auth: {
    localStorage: AsyncStorage,
    autoRefreshToken: true,
    persistSession: true,
    detectSessionInUrl: true,
  },
};
英文:

I had defined localStorage as any because of a tutorial I saw.

I needed to change this:

const options = {
  auth: {
    localStorage: AsyncStorage as any,
    autoRefreshToken: true,
    persistSession: true,
    detectSessionInUrl: true,
  },
};

to this:

const options = {
  auth: {
    localStorage: AsyncStorage,
    autoRefreshToken: true,
    persistSession: true,
    detectSessionInUrl: true,
  },
};

huangapple
  • 本文由 发表于 2023年2月10日 09:47:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定