$userId, 'otp_code' => $otpCode, 'expires_at' => $expiresAt, 'used' => 'F', 'created_at' => date('Y-m-d H:i:s') ]; $db->table(self::$tablename)->insert($data); return $otpCode; } /** * Verify an OTP code for a user * @param int $userId User ID * @param string $otpCode The OTP code to verify * @return bool True if valid, false otherwise */ public static function verifyOTP($userId, $otpCode) { $db = \Config\Database::connect(); // Get the most recent unused OTP for this user $result = $db->table(self::$tablename) ->where('user_id', $userId) ->where('otp_code', $otpCode) ->where('used', 'F') ->where('expires_at >', date('Y-m-d H:i:s')) ->orderBy('created_at', 'DESC') ->limit(1) ->get() ->getRow(); if (!$result) { return false; } // Mark OTP as used $db->table(self::$tablename) ->where('id', $result->id) ->update(['used' => 'T']); return true; } /** * Clean up expired OTP codes * @return int Number of deleted records */ public static function cleanupExpiredOTPs() { $db = \Config\Database::connect(); $db->table(self::$tablename) ->where('expires_at <', date('Y-m-d H:i:s')) ->orWhere('used', 'T') ->delete(); return $db->affectedRows(); } /** * Delete all OTP codes for a user * @param int $userId User ID */ public static function deleteUserOTPs($userId) { $db = \Config\Database::connect(); $db->table(self::$tablename) ->where('user_id', $userId) ->delete(); } }