Overriding TOTP Period Causes Unauthorized / Invalid Two-Factor Cookie Error
When I override the default TOTP period (e.g. from 30 seconds to 60 seconds) in the Better Auth 2FA TOTP plugin configuration. The attempt to verify totp code end up in resulting in Unauthorized response and an Invalid two-factor cookie message. If you dont override the default period. The code works fine.
To Reproduce
I was using Sveltekit when i encountered this error

5 Replies
I'm having the same issue but haven't changed the defaults. Would you mind sharing your implementation @misterh? I'd be curious if you got it to work.
Hi yes, mine started to work when I removed the custom totp period
I can share my code and more details in couples hours when im home
That would be terrific. Thank you @misterh.
this is my auth.ts file and configuration
to enable the totp you need to call
authClient.twoFactor.enable
, its need user account password. and if successfully, it will generate the totp uri, secret and backcode and will add them to the db. you can convert the totp uri into a qr code and let user scan and add it to their authentication app, then prompt user to get the code and call authClient.twoFactor.verifyTotp
and then will enable the totp and 2fa auth for user
What the exact error are you getting @Budi
To enable two-factor authentication (2FA), first call authClient.twoFactor.enable({ password: "userPassword" })
. This will generate a TOTP URI and save the secret and backup codes to the database, but it does not activate 2FA yet. Next, prompt the user to scan the TOTP URI with an authenticator app like Google Authenticator or Authy. Once the user has added the code to their app, ask them to enter the 6-digit code generated by the app. Then call authClient.twoFactor.verifyTotp({ code: "123456" })
. If the verification is successful, 2FA will be fully enabled for the user.Thanks for sharing this. I see you're using the client-side Auth client. I'm trying to do this server-side. I think the issue is that the correct cookies aren't being transmitted from the server-side calls to ensure the verifyTOTP call works.
I discovered the issue was that I wasn't passing headers to my server
signInEmail
API call. Therefore it couldn't receive and set the cookie header.