Skip to content

Feat: Custom OTP PKCE flow + Improve error logging in authentication flow & handle supabase rate-limit error#89

Merged
ben-fornefeld merged 15 commits into
mainfrom
bug-handle-password-reset-rate-limit-error-message-e2b-2619
Jul 6, 2025
Merged

Feat: Custom OTP PKCE flow + Improve error logging in authentication flow & handle supabase rate-limit error#89
ben-fornefeld merged 15 commits into
mainfrom
bug-handle-password-reset-rate-limit-error-message-e2b-2619

Conversation

@ben-fornefeld
Copy link
Copy Markdown
Member

@ben-fornefeld ben-fornefeld commented Jul 3, 2025

This pr features a new custom OTP PKCE flow under /api/auth/confirm, which we can use to route password reset and first time e-mail sign-up e-mail urls to.

Also this pr improves the error logging inside /api/auth/callback and adds explicit error handling for supabase request password rate-limit errors, to inform the user about this state.

The old /api/auth/callback will eventually be removed when we ensure it isn't used anymore.

Migration Strategy
Replace the confirmationUrl value inside our Supbase "Reset Password" and "Confirm Sign-Up", with the following when this pr is deployed:

Reset Password
{{ .SiteURL }}/api/auth/confirm?token_hash={{ .TokenHash }}&type=recovery&next={{ .RedirectTo }}&confirmation_url={{ .ConfirmationURL }}

Confirm Sign-Up
{{ .SiteURL }}/api/auth/confirm?token_hash={{ .TokenHash }}&type=email&next={{ .RedirectTo }}&confirmation_url={{ .ConfirmationURL }}

This commit updates the error handling in the authentication process by replacing `logError` with `console.error` for better visibility of errors during code exchange and password reset actions. Additionally, it removes an unused import of `AUTH_URLS` to streamline the code.
@ben-fornefeld ben-fornefeld self-assigned this Jul 3, 2025
@ben-fornefeld ben-fornefeld added the bug Something isn't working label Jul 3, 2025
@linear
Copy link
Copy Markdown

linear Bot commented Jul 3, 2025

@vercel
Copy link
Copy Markdown

vercel Bot commented Jul 3, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 6, 2025 4:50pm
web-juliett ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 6, 2025 4:50pm


if (error) {
logError(
console.error(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why change from logError > console.error?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the logger is nice when the args do not include a class instance (error in this case). i am about to fix this but in another pr.

if (error) {
console.error(ERROR_CODES.SUPABASE, 'Error resetting password:', error)
if (error.message.includes('security purposes')) {
return returnServerError(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need to return here? returnServerError throws

Copy link
Copy Markdown
Member Author

@ben-fornefeld ben-fornefeld Jul 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, it's just a convenient wrapper i am using because next-safe-action also has returnValidationError().
returnServerError actually also just throws under the hood since this is the way the actionClient is configured

@ben-fornefeld
Copy link
Copy Markdown
Member Author

ben-fornefeld commented Jul 3, 2025

@mishushakov fyi - i will probably extend this pr in the next iterations so, let's not merge yet

This commit updates the authentication confirmation route by introducing better handling of redirect URLs based on the type of authentication request. It adds logging for successful and error cases, ensuring clearer visibility of the authentication process. Additionally, the `createRouteClient` function is modified to accept an optional `NextResponse` parameter, allowing for more flexible cookie management during authentication operations.
@ben-fornefeld ben-fornefeld changed the title Path: Improve error logging in authentication flow & handle supabase rate-limit error Refactor: Custom password reset PCKE flow + Improve error logging in authentication flow & handle supabase rate-limit error Jul 4, 2025
@ben-fornefeld ben-fornefeld changed the title Refactor: Custom password reset PCKE flow + Improve error logging in authentication flow & handle supabase rate-limit error Refactor: Custom password reset PKCE flow + Improve error logging in authentication flow & handle supabase rate-limit error Jul 4, 2025
@ben-fornefeld ben-fornefeld changed the title Refactor: Custom password reset PKCE flow + Improve error logging in authentication flow & handle supabase rate-limit error Feat: Custom password reset PKCE flow + Improve error logging in authentication flow & handle supabase rate-limit error Jul 4, 2025
@ben-fornefeld ben-fornefeld changed the title Feat: Custom password reset PKCE flow + Improve error logging in authentication flow & handle supabase rate-limit error Feat: Custom OTP PKCE flow + Improve error logging in authentication flow & handle supabase rate-limit error Jul 4, 2025
@ben-fornefeld ben-fornefeld marked this pull request as draft July 4, 2025 17:19
This commit improves the logic for determining redirect URLs in the authentication confirmation process. It introduces checks for absolute URLs and refines the handling of the 'next' parameter, ensuring that recovery requests without a specified destination correctly redirect to the password reset page. This change aims to streamline user navigation during authentication flows.
…onfirmation route

This commit enhances the error handling for OTP verification by providing more specific error messages based on the error status. It also updates the response cookie management to support domain-specific settings for absolute redirect URLs, improving the overall user experience during the authentication process.
This PR refactors our authentication flow to improve both user
experience and security.

Key changes
- Hardened redirect logic in the auth callback & confirm routes (origin
checks, graceful fallbacks).
- Enhanced server-side logging for easier debugging of auth issues.
Comment thread src/app/api/auth/confirm/route.ts Outdated
Comment thread src/app/api/auth/confirm/route.ts Outdated
Comment thread src/app/api/auth/confirm/route.ts Outdated
Comment thread src/app/api/auth/confirm/route.ts Outdated
Comment thread src/app/api/auth/confirm/route.ts Outdated
…ameter handling

- Renaming variables for clarity and consistency.
- Adding validation for required parameters before processing.
- Streamlining the redirect logic based on the type of authentication request.
…cation confirmation route

- Introduced Zod schema for validating request parameters.
- Improved error logging for invalid parameters and OTP verification.
- Streamlined redirect logic based on authentication type and next URL handling.
- Added logging for OAuth sign-in actions to improve traceability.
- Introduced a new utility for error handling during the code exchange process, enhancing redirect logic for error scenarios.
…ion route

- Replaced `createRouteClient` with `createClient` for improved client instantiation.
- Removed the deprecated `createRouteClient` function to streamline the codebase.
@ben-fornefeld ben-fornefeld marked this pull request as ready for review July 6, 2025 16:23
@ben-fornefeld ben-fornefeld merged commit 176fc00 into main Jul 6, 2025
4 checks passed
@ben-fornefeld ben-fornefeld deleted the bug-handle-password-reset-rate-limit-error-message-e2b-2619 branch July 6, 2025 18:07
kitchenbeats pushed a commit to kitchenbeats/botlink-dashboard that referenced this pull request Oct 20, 2025
…flow & handle supabase rate-limit error (e2b-dev#89)

This pr features a new custom OTP PKCE flow under `/api/auth/confirm`,
which we can use to route password reset and first time e-mail sign-up
e-mail urls to.

Also this pr improves the error logging inside `/api/auth/callback` and
adds explicit error handling for supabase request password rate-limit
errors, to inform the user about this state.

The old `/api/auth/callback` will eventually be removed when we ensure
it isn't used anymore.

**Migration Strategy**
Replace the confirmationUrl value inside our Supbase "Reset Password"
and "Confirm Sign-Up", with the following when this pr is deployed:

**Reset Password**
`{{ .SiteURL }}/api/auth/confirm?token_hash={{ .TokenHash
}}&type=recovery&next={{ .RedirectTo }}&confirmation_url={{
.ConfirmationURL }}`

**Confirm Sign-Up**
`{{ .SiteURL }}/api/auth/confirm?token_hash={{ .TokenHash
}}&type=email&next={{ .RedirectTo }}&confirmation_url={{
.ConfirmationURL }}`
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

Successfully merging this pull request may close these issues.

2 participants