( kind: DeploymentKind, deploymentId: string, email: string, otp: string )
| 75 | * `verification` table based on the configured storage method. |
| 76 | */ |
| 77 | export async function storeOTP( |
| 78 | kind: DeploymentKind, |
| 79 | deploymentId: string, |
| 80 | email: string, |
| 81 | otp: string |
| 82 | ): Promise<void> { |
| 83 | const keys = OTP_KEYS[kind] |
| 84 | const value = encodeOTPValue(otp, 0) |
| 85 | const storageMethod = getStorageMethod() |
| 86 | |
| 87 | if (storageMethod === 'redis') { |
| 88 | const redis = getRedisClient() |
| 89 | if (!redis) throw new Error('Redis configured but client unavailable') |
| 90 | await redis.set(keys.redisKey(email, deploymentId), value, 'EX', OTP_EXPIRY_SECONDS) |
| 91 | return |
| 92 | } |
| 93 | |
| 94 | const now = new Date() |
| 95 | const expiresAt = new Date(now.getTime() + OTP_EXPIRY_MS) |
| 96 | const identifier = keys.dbIdentifier(email, deploymentId) |
| 97 | |
| 98 | await db.transaction(async (tx) => { |
| 99 | await tx.delete(verification).where(eq(verification.identifier, identifier)) |
| 100 | await tx.insert(verification).values({ |
| 101 | id: generateId(), |
| 102 | identifier, |
| 103 | value, |
| 104 | expiresAt, |
| 105 | createdAt: now, |
| 106 | updatedAt: now, |
| 107 | }) |
| 108 | }) |
| 109 | } |
| 110 | |
| 111 | export async function getOTP( |
| 112 | kind: DeploymentKind, |
no test coverage detected