Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auth.setSession returns AuthRetryableFetchError #1292

Closed
2 tasks done
RostislavGG opened this issue Oct 24, 2024 · 1 comment
Closed
2 tasks done

auth.setSession returns AuthRetryableFetchError #1292

RostislavGG opened this issue Oct 24, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@RostislavGG
Copy link

RostislavGG commented Oct 24, 2024

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

In my react-native expo app I'm using email sign-up with confirmation and a password reset workflow. I'm creating a session from the confirmation URL sent on sign up the following way.

createSessionFromUrl = async (url) => {
        const { params, errorCode } = QueryParams.getQueryParams(url);
        if (errorCode) throw new Error(errorCode);
        const { access_token, refresh_token } = params;
        if (!access_token) return;

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

        if (error) throw error;
    };

In Expo Go everything works fine. The issue comes when I build my app and test it on my device through TestFlight. Upon receiving and going through the confirmation link to the app, receiving the URL and eventually ending up in the mentioned function supabase.auth.setSession returns an error AuthRetryableFetchError with a status: 0. Apart from happening only on production, the other annoying part is that it's not consistent. Every second or third attempt will fail even tho the URL and the tokens are valid.

It's important to note that receiving a link for password reset goes through the same workflow and always succeeds without issues.

To Reproduce

Because of the nature of the issue I can't give any concrete steps. But any suggestion would be welcome.

Expected behavior

setSession, given valid access_token and refresh_token should always succeed

System information

  • OS: iOS
  • Version of supabase-js: "@supabase/supabase-js": "^2.45.6"
  • Expo version: "expo": "^51.0.37"
  • React Native: "react-native": "0.74.5"
@RostislavGG RostislavGG added the bug Something isn't working label Oct 24, 2024
@RostislavGG
Copy link
Author

After speaking with support I was able to mitigate this issue. I'm gonna write down my response and leave it in case someone also stumbles on the same issue.

In short setSession can fail for a multitude of reasons, poor network connection can be one of them.
If we receive an AuthRetryableFetchError we can periodically try again to set the session.

What I ended up doing is this:

const maxRetries = 3;
let attempt = 0;

while (attempt < maxRetries) {
    const { data, error } = await supabase.auth.setSession({
        access_token,
        refresh_token,
    });

    if (!error) {
        return data;
    }

    if (error.name === "AuthRetryableFetchError") {
        attempt += 1;
        await new Promise((resolve) =>
            setTimeout(resolve, 1000 * attempt)
        );
    } else {
        return;
    }
}

Basically try three times to set the session only if it fails with an AuthRetryableFetchError.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant