如何修复使用Supabase身份验证时的水合作用

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

How to fix Hydration with supabase auth

问题

我开始使用NextJS,并且遇到了这个水合错误:
错误:水合失败,因为初始UI与服务器上呈现的内容不匹配。

组件:

"use client";

import { useState, useEffect } from "react";
import { supabase } from "@/lib/supabaseClient";
import { Auth } from "@supabase/auth-ui-react";
import { ThemeSupa } from "@supabase/auth-ui-shared";
import { Session } from "@supabase/supabase-js";

export default function Login() {
  const [session, setSession] = useState<Session | null>(null);

  useEffect(() => {
    supabase.auth.getSession().then(({ data: { session } }) => {
      setSession(session);
    });

    if (typeof window !== "undefined") {
      const {
        data: { subscription },
      } = supabase.auth.onAuthStateChange((_event, session) => {
        setSession(session);
      });

      return () => subscription.unsubscribe();
    }
  }, []);

  if (!session) {
    return (
      <div className="min-h-screen flex flex-col items-center justify-center">
        <div className="w-full max-w-md">
          <Auth
            supabaseClient={supabase}
            appearance={{
              theme: ThemeSupa,
            }}
            providers={[]}
          />
        </div>
      </div>
    );
  }
}

感谢您的帮助! 如何修复使用Supabase身份验证时的水合作用
我想了解为什么会出现水合问题以及如何解决它。

英文:

I started to work with NextJS, and I have this hydration error :
Error: Hydration failed because the initial UI does not match what was rendered on the server.

The Component :

&quot;use client&quot;;

import { useState, useEffect } from &quot;react&quot;;
import { supabase } from &quot;@/lib/supabaseClient&quot;;
import { Auth } from &quot;@supabase/auth-ui-react&quot;;
import { ThemeSupa } from &quot;@supabase/auth-ui-shared&quot;;
import { Session } from &quot;@supabase/supabase-js&quot;;

export default function Login() {
  const [session, setSession] = useState&lt;Session | null&gt;(null);

  useEffect(() =&gt; {
    supabase.auth.getSession().then(({ data: { session } }) =&gt; {
      setSession(session);
    });

    if (typeof window !== &quot;undefined&quot;) {
      const {
        data: { subscription },
      } = supabase.auth.onAuthStateChange((_event, session) =&gt; {
        setSession(session);
      });

      return () =&gt; subscription.unsubscribe();
    }
  }, []);

  if (!session) {
    return (
      &lt;div className=&quot;min-h-screen flex flex-col items-center justify-center&quot;&gt;
        &lt;div className=&quot;w-full max-w-md&quot;&gt;
          &lt;Auth
            supabaseClient={supabase}
            appearance={{
              theme: ThemeSupa,
            }}
            providers={[]}
          /&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    );
  }
}

Thanks for your helping ! 如何修复使用Supabase身份验证时的水合作用

I want to understand why is hydration and how fix it

答案1

得分: 1

可能需要等待组件挂载这个简单的老技巧可能会解决它

```jsx
"use client";

import { useState, useEffect } from "react";
import { supabase } from "@/lib/supabaseClient";
import { Auth } from "@supabase/auth-ui-react";
import { ThemeSupa } from "@supabase/auth-ui-shared";
import { Session } from "@supabase/supabase-js";

export default function Login() {
  const [session, setSession] = useState<Session | null>(null);
  const [isMounted, setIsMounted] = useState(false);

  useEffect(() => {
    setIsMounted(true);

    supabase.auth.getSession().then(({ data: { session } }) => {
      setSession(session);
    });

    if (typeof window !== "undefined") {
      const {
        data: { subscription },
      } = supabase.auth.onAuthStateChange((_event, session) => {
        setSession(session);
      });

      return () => subscription.unsubscribe();
    }
  }, []);

  if (!isMounted) {
    return null;
  }

  if (!session) {
    return (
      <div className="min-h-screen flex flex-col items-center justify-center">
        <div className="w-full max-w-md">
          <Auth
            supabaseClient={supabase}
            appearance={{
              theme: ThemeSupa,
            }}
            providers={[]}
          />
        </div>
      </div>
    );
  }
}
英文:

You might probable have to wait until the component mounts. This simple old trick could fix it.

&quot;use client&quot;;

import { useState, useEffect } from &quot;react&quot;;
import { supabase } from &quot;@/lib/supabaseClient&quot;;
import { Auth } from &quot;@supabase/auth-ui-react&quot;;
import { ThemeSupa } from &quot;@supabase/auth-ui-shared&quot;;
import { Session } from &quot;@supabase/supabase-js&quot;;

export default function Login() {
  const [session, setSession] = useState&lt;Session | null&gt;(null);
  const [isMounted, setIsMounted] = useState(false);

  useEffect(() =&gt; {
    setIsMounted(true);

    supabase.auth.getSession().then(({ data: { session } }) =&gt; {
      setSession(session);
    });

    if (typeof window !== &quot;undefined&quot;) {
      const {
        data: { subscription },
      } = supabase.auth.onAuthStateChange((_event, session) =&gt; {
        setSession(session);
      });

      return () =&gt; subscription.unsubscribe();
    }
  }, []);

  if (!isMounted) {
    return null;
  }

  if (!session) {
    return (
      &lt;div className=&quot;min-h-screen flex flex-col items-center justify-center&quot;&gt;
        &lt;div className=&quot;w-full max-w-md&quot;&gt;
          &lt;Auth
            supabaseClient={supabase}
            appearance={{
              theme: ThemeSupa,
            }}
            providers={[]}
          /&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    );
  }
}

</details>



huangapple
  • 本文由 发表于 2023年6月15日 04:45:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477413.html
匿名

发表评论

匿名网友

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

确定