supabase / auth-js

An isomorphic Javascript library for Supabase Auth.
MIT License
318 stars 152 forks source link

Token has expired or is invalid or duplicate key value violates unique constraint "refresh_tokens_pkey" #916

Closed ksteigerwald closed 3 weeks ago

ksteigerwald commented 3 weeks ago

Bug report

SMS OTP login stopped working. Whenever a customer tries to login they get an error back: duplicate key value violates unique constraint "refresh_tokens_pkey" or Token has Expired.

Describe the bug

I login using OTP via SMS, I get this errors mentioned above

` const handleLoginWithOTP = async () => { const { data, error } = await supabase.auth.signInWithOtp({ phone }); console.log(data, error) if (error) { setErrHeading('Invalid Phone Number') setErrBody(error.message) setAlertState(true) setTimeout(() => { setAlertState(false)}, 5000) return; } setVerify(true); }

const verifyOTP = async () => {
    const {
        data: { session },
        error
    } = await supabase.auth.verifyOtp({
        phone,
        token: token,
        type: 'sms',
        options: {
            redirectTo: `${window.location.origin}/customer/`
        }

    })
    if (error) {
        setErrHeading('Error:');
        setErrBody(error.message);
        setAlertState(true);
        setTimeout(() => { setAlertState(false); }, 5000);
        return;
    }
    if (!session) {
        setErrHeading('Session Not Found');
        setErrBody('A Session was not found on verification, please try to login again.');
        setAlertState(true);
        setTimeout(() => { setAlertState(false); }, 5000);
        return;
    }
    window.location.href = '/customer';
}`

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Go to https://alpine-basecamp.vercel.app/auth
  2. Attempt to login
  3. See error

Expected behavior

For you to be forwarded to the customer page.

Screenshots

If applicable, add screenshots to help explain your problem.

ksteigerwald commented 3 weeks ago

I was able to reset the pk increment using the following command in the sql editor.

-- Reset the sequence for the refresh_tokens table

SELECT setval('auth.refresh_tokens_id_seq', (SELECT MAX(id) FROM auth.refresh_tokens));