Skip to content

Commit 9b9f45b

Browse files
KevenWMarkhamclaude
andcommitted
feat: add hardcoded + environment variable code verification
Implemented flexible access code verification with two methods: Method 1 - Hardcoded List (Option 2): - VALID_CODES array in ApiKeySettings.tsx - Includes expiration dates and descriptions - Perfect for permanent/special access codes - Example codes: 123-4567-890, 999-8888-777, 555-1234-999 Method 2 - Environment Variables (Option 3): - VITE_VALID_ACCESS_CODES environment variable - Comma-separated list of codes - No expiration dates (unlimited validity) - Easy to update without code changes Features: - Validates against both lists - Checks expiration dates for hardcoded codes - Clear validation messages with expiry info - Added .env.example with configuration examples Benefits: - Maximum flexibility for code management - Can combine both methods simultaneously - No backend required (fully client-side) - Easy to manage codes via environment variables 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent aab1490 commit 9b9f45b

File tree

2 files changed

+81
-7
lines changed

2 files changed

+81
-7
lines changed

.env.example

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
1-
# Frontend API URL
2-
VITE_API_URL=http://localhost:3000/api
1+
# Gemini API Configuration
2+
# Your developer API key for Gemini (used in code/paid modes)
3+
VITE_GEMINI_API_KEY=your-api-key-here
4+
5+
# Access Code Verification (Option 3)
6+
# Comma-separated list of valid access codes
7+
# These codes will be accepted in addition to the hardcoded VALID_CODES list
8+
# Example: VITE_VALID_ACCESS_CODES=123-4567-890,999-8888-777,555-1234-999
9+
VITE_VALID_ACCESS_CODES=
10+
11+
# Code Verification API (Option 1 - for backend verification)
12+
# URL for backend API endpoint that verifies access codes
13+
# Example: VITE_CODE_VERIFY_URL=https://your-api.com/api/verify-code
14+
#VITE_CODE_VERIFY_URL=

src/components/ApiKeySettings.tsx

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,34 @@ interface ApiKeySettingsProps {
3434
const API_KEY_STORAGE_KEY = 'gemini_api_config'
3535
const CODE_PATTERN = /^\d{3}-\d{4}-\d{3}$/ // Format: 000-0000-000
3636

37+
// Valid access codes with expiration dates and descriptions
38+
// Option 2: Hardcoded list (for quick testing/permanent codes)
39+
const VALID_CODES = [
40+
{
41+
code: '123-4567-890',
42+
expiresAt: '2025-12-31',
43+
description: 'Beta Tester Access',
44+
},
45+
{
46+
code: '999-8888-777',
47+
expiresAt: '2025-06-30',
48+
description: 'Promotional Code',
49+
},
50+
{
51+
code: '555-1234-999',
52+
expiresAt: null,
53+
description: 'Lifetime Access',
54+
},
55+
]
56+
57+
// Option 3: Load additional codes from environment variable
58+
// Set VITE_VALID_ACCESS_CODES=123-4567-890,999-8888-777,555-1234-999 in .env
59+
const getValidCodesFromEnv = (): string[] => {
60+
const envCodes = import.meta.env.VITE_VALID_ACCESS_CODES
61+
if (!envCodes) return []
62+
return envCodes.split(',').map((code: string) => code.trim())
63+
}
64+
3765
export function ApiKeySettings({
3866
isOpen,
3967
onClose,
@@ -94,11 +122,45 @@ export function ApiKeySettings({
94122
return false
95123
}
96124

97-
// Code format is valid - mark as valid
98-
// In a production app, you would verify this code against a database
99-
setValidationStatus('valid')
100-
setValidationMessage('Access code format is valid!')
101-
return true
125+
// Check Option 2: Hardcoded list with expiration dates
126+
const validCode = VALID_CODES.find(c => c.code === accessCode)
127+
128+
if (validCode) {
129+
// Check if code is expired
130+
if (validCode.expiresAt) {
131+
const expiryDate = new Date(validCode.expiresAt)
132+
if (expiryDate < new Date()) {
133+
setValidationStatus('invalid')
134+
setValidationMessage('This access code has expired.')
135+
return false
136+
}
137+
}
138+
139+
// Code is valid!
140+
setValidationStatus('valid')
141+
const expiryMessage = validCode.expiresAt
142+
? ` Valid until ${new Date(validCode.expiresAt).toLocaleDateString()}.`
143+
: ' No expiration.'
144+
setValidationMessage(
145+
`Access code verified! ${validCode.description}.${expiryMessage}`
146+
)
147+
return true
148+
}
149+
150+
// Check Option 3: Environment variable codes (no expiration)
151+
const envCodes = getValidCodesFromEnv()
152+
if (envCodes.includes(accessCode)) {
153+
setValidationStatus('valid')
154+
setValidationMessage('Access code verified! No expiration.')
155+
return true
156+
}
157+
158+
// Code not found in either list
159+
setValidationStatus('invalid')
160+
setValidationMessage(
161+
'Invalid access code. Please contact support for a valid code.'
162+
)
163+
return false
102164
}
103165

104166
const validateApiKey = async () => {

0 commit comments

Comments
 (0)