feat: single api for oauth connections#741
Conversation
Merge pull request Mail-0#737 from Mail-0/main
|
@BlankParticle is attempting to deploy a commit to the Zero Team on Vercel. A member of the Team first needs to authorize it. |
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughThis change removes custom OAuth authentication API route handlers for mail provider connection initiation and callback, and migrates the connection flow to use a new programmatic approach. The email provider connection UI is updated to initiate linking via a client-side method instead of navigation. Authentication and connection logic is refactored into a new hook function, which is registered with the authentication library and runs after account creation or update. The email provider constants are made immutable, and the "better-auth" dependency is updated to a newer version. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant AddConnectionDialog
participant authClient
participant betterAuth
participant Database
User->>AddConnectionDialog: Clicks provider button
AddConnectionDialog->>authClient: linkSocial(providerId)
authClient->>betterAuth: Initiate OAuth flow
betterAuth->>User: Redirect to provider for authentication
User->>betterAuth: Completes authentication
betterAuth->>connectionHandlerHook: After account create/update
connectionHandlerHook->>Database: Insert/Update connection record
betterAuth->>User: Redirect to mail page
Suggested reviewers
Poem
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
bun.lockis excluded by!**/*.lock
📒 Files selected for processing (6)
apps/mail/app/api/v1/mail/auth/[providerId]/callback/route.ts(0 hunks)apps/mail/app/api/v1/mail/auth/[providerId]/init/route.ts(0 hunks)apps/mail/components/connection/add.tsx(3 hunks)apps/mail/lib/auth.ts(3 hunks)apps/mail/lib/constants.ts(1 hunks)apps/mail/package.json(1 hunks)
💤 Files with no reviewable changes (2)
- apps/mail/app/api/v1/mail/auth/[providerId]/callback/route.ts
- apps/mail/app/api/v1/mail/auth/[providerId]/init/route.ts
🧰 Additional context used
🧬 Code Graph Analysis (1)
apps/mail/lib/auth.ts (4)
packages/db/src/schema.ts (2)
account(32-48)connection(77-94)apps/mail/types/index.ts (1)
Account(7-11)apps/mail/app/api/driver/google.ts (1)
driver(127-877)packages/db/src/index.ts (1)
db(17-17)
🔇 Additional comments (7)
apps/mail/package.json (1)
63-63: Dependency upgrade aligns with new features.The upgraded "better-auth" version (1.2.1 → 1.2.7) supports the new account linking and hook features implemented in the auth.ts file.
apps/mail/lib/constants.ts (1)
100-106: Improved type safety with 'as const'.The addition of
as constassertion turns the array into a readonly tuple with literal types, providing stronger type checking and better IDE support for the provider IDs used throughout the authentication flow.apps/mail/components/connection/add.tsx (2)
10-10: New dependency imported to support programmatic OAuth flow.The import of
authClientenables programmatic OAuth connection instead of navigation-based flows.
56-78: Transition from link-based to programmatic OAuth flow.The implementation has been updated to replace anchor elements with div elements and use a programmatic approach for OAuth connection. This change:
- Eliminates the need for separate API routes for each provider
- Centralizes the OAuth flow to use the "better-auth" library directly
- Maintains the same user experience with animations and visual elements
Let's verify that all required error handling is in place:
#!/bin/bash # Check for error handling in the authClient.linkSocial method rg -A 5 "linkSocial" --type tsapps/mail/lib/auth.ts (3)
4-4: Updated imports to support new authentication hooks.The modified imports bring in necessary types and functions to support the new connection handling functionality, including:
- Account type from better-auth
- createDriver function to instantiate OAuth provider drivers
- APIError for proper error handling
Also applies to: 9-9, 11-11
70-70: Enhanced database adapter configuration.The database adapter configuration has been formatted more clearly, making it easier to read and maintain.
81-97: Robust account linking configuration.The new configuration enables secure account linking with these key features:
- Allows users to link accounts with different email addresses, improving flexibility
- Restricts trusted providers to only 'google', enhancing security
- Registers the connection handler to run after account creation and update events
This configuration directly addresses the PR objectives of preventing duplicate connections and allowing login with any connected email address.
Let's verify that all files have been updated consistently with the new approach:
#!/bin/bash # Check if any remaining OAuth route handlers might still exist fd -t f -e ts "route.ts" --exec grep -l "mail/auth/\[providerId\]" {}
|
Hey @BlankParticle can you check the coderabbitai comments please? |
435c38f to
2763b45
Compare
I have added extra error handling for the |
ahmetskilinc
left a comment
There was a problem hiding this comment.
@MrgSub approving this - tested locally.
Description
Instead of having 2 endpoints, one for better auth and one custom, consolidate the oauth part into better auth and update relevant tables using hooks.
This fixes the following issues.
Type of Change
Please delete options that are not relevant.
Areas Affected
Please check all that apply:
Testing Done
Describe the tests you've done:
Security Considerations
For changes involving data or authentication:
Checklist
Additional Notes
Add any other context about the pull request here.
Screenshots/Recordings
Add screenshots or recordings here if applicable.
By submitting this pull request, I confirm that my contribution is made under the terms of the project's license.
Summary by CodeRabbit
New Features
Refactor
Chores