Supabase使用魔术链接登录时未调用onAuthStateChange。

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

Supabase sign in with magic link not calling onAuthStateChange

问题

I'm using react native and supabase

I call

await supabase.auth.signInWithOtp({
email: email,
options: {
emailRedirectTo: 'myURL',
},
})

and this successfully sends the user an email.

the user clicks the email. I can see that the user is created in the supabase dashboard and email is verified

however in my app supabase.auth.onAuthStateChange method is never fired

also upon returning to the app I try and call await supabase.auth.getUser() but I get this:

{"data":{"user":null},"error":{"name":"AuthApiError","message":"invalid claim: missing sub claim","status":401}}

so not sure whats going on. Again the user is created in the supabase dashboard. They click the link in the email, it opens my app. i just get no call back

英文:

I'm using react native and supabase

I call

await supabase.auth.signInWithOtp({
        email: email,
        options: {
          emailRedirectTo: 'myURL',
        },
      })

and this successfully sends the user an email.

the user clicks the email. I can see that the user is created in the supabase dashboard and email is verified

however in my app supabase.auth.onAuthStateChange method is never fired

also upon returning to the app I try and call await supabase.auth.getUser() but I get this:

{"data":{"user":null},"error":{"name":"AuthApiError","message":"invalid claim: missing sub claim","status":401}}

so not sure whats going on. Again the user is created in the supabase dashboard. They click the link in the email, it opens my app. i just get no call back

答案1

得分: 2

以下是翻译好的部分:

解决方案是在React Native上设置深层链接

https://reactnavigation.org/docs/deep-linking/

然后监听链接的打开,解析链接,并根据解析的内容手动设置会话。

useEffect(() => {
  const handleUrlRedirect = async (url) => {
    console.log('Received URL:', url);
    if (!url) return;

    // 提取 '#' 后面的部分
    const fragment = url.split('#')[1];
    if (!fragment) return;

    // 将片段转换为对象
    const params = new URLSearchParams(fragment);
    const access_token = params.get('access_token');
    const refresh_token = params.get('refresh_token');

    if (
      typeof access_token !== 'string' ||
      typeof refresh_token !== 'string'
    ) {
      console.log('发生错误。电子邮件链接仅有效一次。请尝试请求另一个链接。')
      return;
    }

    try {

      supabase.auth.onAuthStateChange((event, session) => {
        console.log(event, session)
      })

       var data = await supabase.auth.setSession({
        access_token,
        refresh_token,
      });

    } catch (e) {
      console.error(error);
    }
  };

  Linking.getInitialURL().then((url) => {
    handleUrlRedirect(url);
  });

  const subscription = Linking.addEventListener('url', ({ url }) => {
    handleUrlRedirect(url);
  });

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

请注意,代码部分未进行翻译。如果需要进一步的翻译或解释,请告诉我。

英文:

The solution was to set up deep linking on react native

https://reactnavigation.org/docs/deep-linking/

Then listen for the link opening, parse the link, and manually set the session based on what was parsed

useEffect(() => {
  const handleUrlRedirect = async (url) => {
    console.log('Received URL:', url);
    if (!url) return;
  
    // Extract the part after the '#'
    const fragment = url.split('#')[1];
    if (!fragment) return;
  
    // Convert the fragment into an object
    const params = new URLSearchParams(fragment);
    const access_token = params.get('access_token');
    const refresh_token = params.get('refresh_token');
  
    if (
      typeof access_token !== 'string' ||
      typeof refresh_token !== 'string'
    ) {
      console.log('An Error occurred. Email links are only valid for 1 click. Try requesting another link.')

      return;
    }
  
    try {

      supabase.auth.onAuthStateChange((event, session) => {
        console.log(event, session)
      })

       var data = await supabase.auth.setSession({
        access_token,
        refresh_token,
      });

    } catch (e) {
      console.error(error);
    }
  };

  Linking.getInitialURL().then((url) => {
    handleUrlRedirect(url);
  });

  const subscription = Linking.addEventListener('url', ({ url }) => {
    handleUrlRedirect(url);
  });

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

huangapple
  • 本文由 发表于 2023年5月14日 04:22:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244720.html
匿名

发表评论

匿名网友

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

确定