Supabase Auth:Google Oauth 成功,但未触发 onAuthStateChange

问题描述 投票:0回答:1

我正在构建一个 Google 幻灯片插件。数据库操作使用 Supabase 实现,前端使用 React 实现。

我正在尝试使用 Supabase oauth 通过 Google 对用户进行身份验证。

这是我的代码:

const handleSignIn = async () => {
    const { data, error } = await supabaseClient.auth.signInWithOAuth({
      provider: 'google',
      options: {
        skipBrowserRedirect: true,
        redirectTo: 'https://<domain>.com/success',
      },
    });

    if (error || !data || !data.url) {
      console.error('Error signing in:', error);
      return;
    }

    // open google oauth consent screen in new tab
    // without this it tries to open the iframe inside the modal which Google doesn't allow
    // check https://stackoverflow.com/questions/71819128/supabase-auth-onauthstatechange-not-working-when-react-app-is-in-iframe
    window.open(data.url, '_blank');
  };

用户单击链接,即可登录。Supabase 在

auth.users
表中创建用户身份。但是,我无法获取登录用户的详细信息。在我的顶级组件中,我有这个:

  const [session, setSession] = useState<Session | null>(null);

  useEffect(() => {
    supabaseClient.auth.getSession().then(({ data: { session } }) => {
      console.log('got session', session);
      setSession(session);
    });

    supabaseClient.auth.onAuthStateChange((event, session) => {
      console.log('authChanged', event, session);
      setSession(session);
    });
  }, []);

会话始终为空,我只看到

INITIAL_SESSION
的 1 个控制台日志,该日志为空。当用户登录或退出时,我看不到任何其他身份验证状态更改。

如何获取登录用户的身份验证状态更改?

reactjs oauth-2.0 supabase supabase-js
1个回答
0
投票

我的情况的问题是,我路由到的页面在 Supabase 处理它之前就剥离了访问令牌

© www.soinside.com 2019 - 2024. All rights reserved.