feat(auth): dedicated EmailOtp model - #60
Open
Tijesunimi004 wants to merge 1 commit into
Open
Conversation
Replace the emailOtp/emailOtpExpiresAt pair on Merchant with an EmailOtp model shaped like AuthNonce: one row per generated code, hashed, spent by stamping usedAt. This gives OTP history and makes the "1 resend per minute" rule a plain query for a recent row instead of arithmetic against a single mutable expiry field. - schema: add EmailOtp (merchantId FK, codeHash, expiresAt, usedAt, createdAt; index on merchantId+createdAt); drop the two Merchant fields - otp.services: issueEmailOtp inserts a row; verifyEmailOtp matches the newest unused row, checks expiry and hash, stamps usedAt, then flips Merchant.emailVerified; resendEmailOtp rate-limits via an EmailOtp createdAt lookup - merchant.services: registerMerchant delegates OTP issuance to issueEmailOtp instead of duplicating generate/hash/send inline - migration drops in-flight codes, which is acceptable Response codes for wrong, expired, and already-used codes are unchanged.
Contributor
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 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. Comment |
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.
Closes #28
What changed
Email OTP verification stored the hashed code and expiry as
emailOtp/emailOtpExpiresAtcolumns onMerchant, overwritten in place on each issue/resend. This moves OTP storage into its ownEmailOtpmodel, shaped like the existingAuthNoncemodel: one row per generated code, spent by stampingusedAtrather than nulling a shared field.Schema
EmailOtpmodel:id,merchantId(FK toMerchant),codeHash,expiresAt,usedAt?,createdAt, with an index on(merchantId, createdAt).Merchant.emailOtpandMerchant.emailOtpExpiresAt.onDelete: Cascade— OTP rows are ephemeral and should not block a merchant delete.AuthNonceusesSET NULLonly because itsmerchantIdis nullable; here it is required, matching the issue's schema.Services
issueEmailOtpinserts a newEmailOtprow instead of writing toMerchant.verifyEmailOtplooks up the merchant's most recent unused row, checks expiry then hash, stampsusedAton success, and setsMerchant.emailVerified. Wrong / expired / missing codes return the same 400s as before.resendEmailOtpenforces the one-per-minute limit by querying for anEmailOtprow created within the last 60s, instead of deriving "last sent" fromexpiresAt - 10min.registerMerchantnow callsissueEmailOtprather than duplicating the generate/hash/send logic inline.email.service.ts.Tests
otp.servicesandauth.email-otpsuites against the new model, including expired and already-used rows.registerMerchantsuites to assertissueEmailOtpis invoked.emailOtpfields from shared merchant fixtures.Full suite: 507 passing.