feat(chat-otp): added db fallback for chat otp#2582
Merged
waleedlatif1 merged 2 commits intostagingfrom Dec 25, 2025
Merged
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
Contributor
Greptile SummaryReplaces unreliable in-memory OTP storage with database-backed fallback using the existing Key changes:
Benefits:
Confidence Score: 4/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Client
participant API as OTP Route
participant Storage as getStorageMethod()
participant Redis
participant DB as Database (verification)
participant Email as Email Service
Note over Client,Email: POST - Request OTP
Client->>API: POST /api/chat/[identifier]/otp<br/>{email}
API->>DB: Query chat by identifier
DB-->>API: Return chat config
API->>API: Validate email against allowedEmails
API->>API: Generate 6-digit OTP
API->>Storage: Check storage method
alt Redis Storage
Storage-->>API: "redis"
API->>Redis: SET otp:email:chatId (EX 900)
Redis-->>API: OK
else Database Storage
Storage-->>API: "database"
API->>DB: BEGIN TRANSACTION
API->>DB: DELETE verification WHERE identifier=chat-otp:chatId:email
API->>DB: INSERT verification (id, identifier, value, expiresAt)
API->>DB: COMMIT TRANSACTION
DB-->>API: Success
end
API->>Email: Send OTP email
Email-->>API: Success
API-->>Client: 200 {message: "Verification code sent"}
Note over Client,Email: PUT - Verify OTP
Client->>API: PUT /api/chat/[identifier]/otp<br/>{email, otp}
API->>DB: Query chat by identifier
DB-->>API: Return chat config
API->>Storage: Check storage method
alt Redis Storage
Storage-->>API: "redis"
API->>Redis: GET otp:email:chatId
Redis-->>API: stored OTP
else Database Storage
Storage-->>API: "database"
API->>DB: SELECT * FROM verification<br/>WHERE identifier=chat-otp:chatId:email<br/>AND expiresAt > now
DB-->>API: Return OTP record
end
API->>API: Compare stored OTP with provided OTP
alt OTP Valid
API->>Storage: Check storage method
alt Redis Storage
API->>Redis: DEL otp:email:chatId
else Database Storage
API->>DB: DELETE verification WHERE identifier
end
API->>API: Set auth cookie
API-->>Client: 200 {authenticated: true}
else OTP Invalid
API-->>Client: 400 {error: "Invalid verification code"}
end
|
Collaborator
Author
|
@greptile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Type of Change
Testing
Tested manually
Checklist