-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(two-factor): add twoFactorPage
config
#5329
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
base: canary
Are you sure you want to change the base?
feat(two-factor): add twoFactorPage
config
#5329
Conversation
…gin and its logic to handle redirecting users to the specified page when 2FA verification is required
…nfiguration for redirecting users during verification
@wuzgood98 is attempting to deploy a commit to the better-auth Team on Vercel. A member of the Team first needs to authorize it. |
better-auth
@better-auth/cli
@better-auth/core
@better-auth/expo
@better-auth/sso
@better-auth/stripe
@better-auth/telemetry
commit: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No issues found across 2 files
if (context.data?.twoFactorRedirect) { | ||
if (options?.twoFactorPage) { | ||
if (typeof window !== "undefined" && window.location) { | ||
window.location.href = options.twoFactorPage; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One issue here is that it causes a full page reload. In frameworks like NextJS for instance, it may be more desirable to use Next's own router.push() instead.
The current method of setting onSuccess at the point of calling signIn on the client supports this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your suggestion! I understand your concern about full page reload. However, I have decided to keep the implementation as a fallback rather than use Next.js's router.push()
for one reason:
- Since Better-Auth is framework-agnostic and supports many environments, hardcoding Next.js's router will create a dependency on a specific framework, which would either require duplicate logic for each framework or force all users to implement framework-specific workarounds.
My Implementation:
onTwoFactorRedirect
takes priority (so framework-specific routing works perfectly)twoFactorPage
serves as a simple fallback with a clear@warning
that it causes a full page reload
This gives users flexibility while maintaining the library's framework-agnostic design
Users who want smooth client-side navigation get it through the callback, while those who need a quick fallback have twoFactorPage
.
…urs after custom callback execution, with added warning in documentation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No issues found across 1 file
…ge` option and suggest `onTwoFactorRedirect` for smoother user experience
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No issues found across 1 file
What changes were made and why:
twoFactorClient
to accepttwoFactorPage
.twoFactorPage
is provided.twoFactorPage
oronTwoFactorRedirect
to redirect if a user needs to verify their two factor.Relevant context:
twoFactorClient
does not accepttwoFactorPage
as an option.onTwoFactorRedirect
which was not used in the docs on its basic usage.Breaking changes / depreciations:
Screenshots for UI changes:
Related issues / discussions:
Summary by cubic
Added a twoFactorPage config to twoFactorClient to automatically redirect users to a 2FA verification page when needed. onTwoFactorRedirect remains supported as a fallback, and docs were updated so the basic usage example works without TypeScript errors.