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

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

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

问题

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

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

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

  1. const options = {
  2. auth: {
  3. localStorage: AsyncStorage as any,
  4. autoRefreshToken: true,
  5. persistSession: true,
  6. detectSessionInUrl: true,
  7. },
  8. };
  9. const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY, options);
  10. export { supabase };

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

  1. export function HomeScreen() {
  2. const [displayName, setDisplayName] = useState();
  3. useEffect(() => {
  4. (async () => {
  5. const user = (await supabase.auth.getSession()).data.session?.user;
  6. console.log("User: " + user);
  7. const { data, error } = await supabase
  8. .from("profiles")
  9. .select()
  10. .eq("uid", user?.id);
  11. if (error === null) {
  12. data!.map((data) => {
  13. const name = data.displayName;
  14. console.log(name);
  15. setDisplayName(name);
  16. });
  17. } else {
  18. console.log(error.message);
  19. }
  20. console.log("Name: " + displayName);
  21. })();
  22. }, [setDisplayName]);
  23. return (
  24. <View>
  25. <Text>{displayName}</Text>
  26. </View>
  27. );
  28. }

请注意,代码中的一些标记(如&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.

  1. const options = {
  2. auth: {
  3. localStorage: AsyncStorage as any,
  4. autoRefreshToken: true,
  5. persistSession: true,
  6. detectSessionInUrl: true,
  7. },
  8. };
  9. const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY, options);
  10. 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.

  1. export function HomeScreen() {
  2. const [displayName, setDisplayName] = useState();
  3. useEffect(() =&gt; {
  4. (async () =&gt; {
  5. const user = (await supabase.auth.getSession()).data.session?.user;
  6. console.log(&quot;User: &quot; + user);
  7. const { data, error } = await supabase
  8. .from(&quot;profiles&quot;)
  9. .select()
  10. .eq(&quot;uid&quot;, user?.id);
  11. if (error === null) {
  12. data!.map((data) =&gt; {
  13. const name = data.displayName;
  14. console.log(name);
  15. setDisplayName(name);
  16. });
  17. } else {
  18. console.log(error.message);
  19. }
  20. console.log(&quot;Name: &quot; + displayName);
  21. })();
  22. }, [setDisplayName]);
  23. return (
  24. &lt;View&gt;
  25. &lt;Text&gt;{displayName}&lt;/Text&gt;
  26. &lt;/View&gt;
  27. );
  28. }

答案1

得分: 0

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

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

  1. const options = {
  2. auth: {
  3. localStorage: AsyncStorage as any,
  4. autoRefreshToken: true,
  5. persistSession: true,
  6. detectSessionInUrl: true,
  7. },
  8. };

改成这样:

  1. const options = {
  2. auth: {
  3. localStorage: AsyncStorage,
  4. autoRefreshToken: true,
  5. persistSession: true,
  6. detectSessionInUrl: true,
  7. },
  8. };
英文:

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

I needed to change this:

  1. const options = {
  2. auth: {
  3. localStorage: AsyncStorage as any,
  4. autoRefreshToken: true,
  5. persistSession: true,
  6. detectSessionInUrl: true,
  7. },
  8. };

to this:

  1. const options = {
  2. auth: {
  3. localStorage: AsyncStorage,
  4. autoRefreshToken: true,
  5. persistSession: true,
  6. detectSessionInUrl: true,
  7. },
  8. };

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:

确定