Skip to content

Commit 8a0c7e9

Browse files
committed
re-added premium tier checking and moved token validation after user input validation
1 parent 03f72ed commit 8a0c7e9

File tree

2 files changed

+100
-34
lines changed

2 files changed

+100
-34
lines changed

src/signup/index.js

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ exports.handler = async (event, context) => {
4545

4646
const { email, walletAddress, tier = 'free', turnstileToken } = body;
4747

48+
// Validate inputs
49+
const validationError = validateParams({ email, walletAddress }, ['email', 'walletAddress']);
50+
if (validationError) {
51+
return formatResponse(
52+
validationError.statusCode,
53+
{
54+
error: validationError.error,
55+
message: validationError.error
56+
},
57+
process.env.CORS_ORIGIN
58+
);
59+
}
60+
4861
// Validate Turnstile token for free tier
4962
if (tier.toLowerCase() === 'free') {
5063
if (!turnstileToken) {
@@ -83,19 +96,6 @@ exports.handler = async (event, context) => {
8396
});
8497
}
8598

86-
// Validate inputs
87-
const validationError = validateParams({ email, walletAddress }, ['email', 'walletAddress']);
88-
if (validationError) {
89-
return formatResponse(
90-
validationError.statusCode,
91-
{
92-
error: validationError.error,
93-
message: validationError.error
94-
},
95-
process.env.CORS_ORIGIN
96-
);
97-
}
98-
9999
if (!validateEmail(email)) {
100100
return formatResponse(
101101
400,
@@ -128,30 +128,53 @@ exports.handler = async (event, context) => {
128128
);
129129
}
130130

131-
// For Free tier, create and send API key immediately
132-
const clientIP = getClientIP(event);
133-
const user = await createUserWithApiKey(normalizedEmail, normalizedWallet, 'free', clientIP);
131+
if (tier.toLowerCase() === 'premium') {
132+
// For Premium tier, redirect to payment flow
133+
const clientIP = getClientIP(event);
134+
135+
// Log premium tier signup initiation
136+
console.log('Premium tier signup initiated:', {
137+
email: normalizedEmail,
138+
walletAddress: normalizedWallet,
139+
tier: 'premium',
140+
clientIP: clientIP
141+
});
134142

135-
// Log successful user account creation
136-
console.log('User account created successfully:', {
137-
email: normalizedEmail,
138-
walletAddress: normalizedWallet,
139-
tier: 'free',
140-
clientIP: clientIP
141-
});
143+
return formatResponse(
144+
200,
145+
{
146+
message: 'Redirect to payment flow',
147+
redirectUrl: `${process.env.PAYMENT_URL}?email=${encodeURIComponent(normalizedEmail)}&wallet=${encodeURIComponent(normalizedWallet)}`
148+
},
149+
process.env.CORS_ORIGIN
150+
);
151+
}
152+
else {
153+
// For Free tier, create and send API key immediately
154+
const clientIP = getClientIP(event);
155+
const user = await createUserWithApiKey(normalizedEmail, normalizedWallet, 'free', clientIP);
156+
157+
// Log successful user account creation
158+
console.log('User account created successfully:', {
159+
email: normalizedEmail,
160+
walletAddress: normalizedWallet,
161+
tier: 'free',
162+
clientIP: clientIP
163+
});
142164

143-
// Send welcome email with API key
144-
await sendWelcomeEmail(normalizedEmail, user.apiKey);
165+
// Send welcome email with API key
166+
await sendWelcomeEmail(normalizedEmail, user.apiKey);
145167

146-
// Return success response
147-
return formatResponse(
148-
200,
149-
{
150-
message: 'API key created successfully',
151-
apiKey: user.apiKey
152-
},
153-
process.env.CORS_ORIGIN
154-
);
168+
// Return success response
169+
return formatResponse(
170+
200,
171+
{
172+
message: 'API key created successfully',
173+
apiKey: user.apiKey
174+
},
175+
process.env.CORS_ORIGIN
176+
);
177+
}
155178
}
156179
catch (error) {
157180
console.error('Error processing signup:', error);

test/signup.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,27 @@ describe('Signup Lambda Function', () => {
111111
expect(email.sendWelcomeEmail).toHaveBeenCalledWith('[email protected]', 'test-api-key');
112112
});
113113

114+
it('should redirect to payment flow for premium tier', async () => {
115+
const event = {
116+
httpMethod: 'POST',
117+
body: JSON.stringify({
118+
119+
walletAddress: '0x1234567890abcdef1234567890abcdef12345678',
120+
tier: 'premium'
121+
})
122+
};
123+
124+
const response = await handler(event);
125+
const body = JSON.parse(response.body);
126+
127+
expect(response.statusCode).toBe(200);
128+
expect(body.message).toBe('Redirect to payment flow');
129+
expect(body.redirectUrl).toBeDefined();
130+
131+
// Should not create API key or send email for premium tier
132+
expect(apiKey.createUserWithApiKey).not.toHaveBeenCalled();
133+
expect(email.sendWelcomeEmail).not.toHaveBeenCalled();
134+
});
114135

115136
it('should return 400 for invalid email', async () => {
116137
const event = {
@@ -265,4 +286,26 @@ describe('Signup Lambda Function', () => {
265286
expect(body.error).toBe('Missing verification');
266287
expect(body.field).toBe('turnstile');
267288
});
289+
290+
it('should validate parameters before checking turnstile token', async () => {
291+
const event = {
292+
httpMethod: 'POST',
293+
body: JSON.stringify({
294+
295+
// walletAddress is missing
296+
tier: 'free',
297+
turnstileToken: 'valid-token'
298+
})
299+
};
300+
301+
const response = await handler(event);
302+
const body = JSON.parse(response.body);
303+
304+
expect(response.statusCode).toBe(400);
305+
expect(body.error).toBe('Missing parameter: walletAddress');
306+
307+
// Verify turnstile verification was not called since params failed first
308+
const TurnstileVerificationInstance = new TurnstileVerification();
309+
expect(TurnstileVerificationInstance.verifyToken).not.toHaveBeenCalled();
310+
});
268311
});

0 commit comments

Comments
 (0)