-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
1598 lines (1356 loc) · 54.4 KB
/
server.js
File metadata and controls
1598 lines (1356 loc) · 54.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const express = require('express');
const cors = require('cors');
const axios = require('axios');
const { Pool } = require('pg');
const crypto = require('crypto');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 8080;
// Parse allowed origins from environment variable
const allowedOrigins = process.env.ALLOWED_ORIGINS
? process.env.ALLOWED_ORIGINS.split(',').map(origin => origin.trim())
: ['http://0.0.0.0:8080'];
// Parse paid member tags from environment variable
const paidMemberTags = process.env.PAID_MEMBER_TAGS
? process.env.PAID_MEMBER_TAGS.split(',').map(tag => tag.trim().toLowerCase())
: ['paid', 'premium', 'subscriber', 'member', 'vip', 'pro'];
// Database connection pool
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});
// Test database connection
pool.on('connect', () => {
console.log('Connected to the PostgreSQL database');
});
pool.on('error', (err) => {
console.error('Database connection error:', err);
});
if (process.env.NODE_ENV !== 'production') {
allowedOrigins.push('http://0.0.0.0:8080');
allowedOrigins.push('http://0.0.0.0:3000');
}
console.log('Allowed CORS origins:', allowedOrigins);
console.log('Paid member tags:', paidMemberTags);
console.log('Database URL configured:', process.env.DATABASE_URL ? 'Yes' : 'No');
// In-memory store for temporary admin tokens (in production, use Redis)
const adminTokens = new Map();
// Circle Admin API configuration
const CIRCLE_ADMIN_API_TOKEN = process.env.CIRCLE_ADMIN_API_TOKEN;
const CIRCLE_ADMIN_API_BASE = 'https://app.circle.so';
// Function to get admin tag ID by name
async function getAdminTagIdByName(tagName) {
if (!CIRCLE_ADMIN_API_TOKEN) {
console.log('CIRCLE_ADMIN_API_TOKEN not configured, skipping tag management');
return null;
}
try {
// Get all admin tags
const response = await axios.get(
`${CIRCLE_ADMIN_API_BASE}/api/admin/v2/member_tags`,
{
headers: {
'Authorization': `Bearer ${CIRCLE_ADMIN_API_TOKEN}`,
'Content-Type': 'application/json'
}
}
);
console.log('Admin tags API response:', JSON.stringify(response.data, null, 2));
// The response structure is { page: 1, records: [...] }
const tags = response.data?.records || response.data || [];
if (!Array.isArray(tags)) {
console.error('Unexpected tags response format:', typeof tags);
return null;
}
// Find the tag with matching name
const adminTag = tags.find(tag => tag.name === tagName);
if (adminTag) {
console.log(`Found admin tag: name="${adminTag.name}", admin_id=${adminTag.id}`);
return adminTag.id;
} else {
console.log(`Admin tag with name "${tagName}" not found in ${tags.length} tags`);
return null;
}
} catch (error) {
console.error('Error fetching admin tags:', error.response?.data || error.message);
return null;
}
}
// Function to delete a member tag using Circle Admin API
async function deleteMemberTag(userEmail, memberTagId) {
if (!CIRCLE_ADMIN_API_TOKEN) {
console.log('CIRCLE_ADMIN_API_TOKEN not configured, cannot delete tag');
return false;
}
try {
console.log(`Attempting to delete tag: email=${userEmail}, tag_id=${memberTagId}`);
const response = await axios.delete(
`${CIRCLE_ADMIN_API_BASE}/api/admin/v2/tagged_members`,
{
params: {
user_email: userEmail,
member_tag_id: memberTagId
},
headers: {
'Authorization': `Bearer ${CIRCLE_ADMIN_API_TOKEN}`,
'Content-Type': 'application/json'
}
}
);
console.log(`Successfully deleted member tag ID ${memberTagId} for user ${userEmail}:`, response.data);
return true;
} catch (error) {
console.error('Error deleting member tag:', error.response?.data || error.message);
if (error.response?.status) {
console.error('Response status:', error.response.status);
}
return false;
}
}
app.use(cors({
origin: function(origin, callback) {
// Allow requests with no origin (same-origin requests, server-side requests, mobile apps, etc.)
if (!origin) {
// Log only in development
if (process.env.NODE_ENV !== 'production') {
console.log('Request with no origin header (likely same-origin or server-side)');
}
return callback(null, true);
}
// Check if origin is in allowed list
if (allowedOrigins.indexOf(origin) !== -1) {
return callback(null, true);
}
// In development, be more permissive
if (process.env.NODE_ENV !== 'production') {
console.log('Development mode: allowing origin:', origin);
return callback(null, true);
}
// In production, block unknown origins
console.warn('CORS blocked origin:', origin);
return callback(new Error('Not allowed by CORS'));
},
credentials: true
}));
app.use(express.json());
app.use(express.static('public'));
// Endpoint to provide client configuration
app.get('/api/config', (req, res) => {
res.json({
allowedOrigins: allowedOrigins,
requireIframe: process.env.REQUIRE_IFRAME === 'true',
disableEmailOnlyAuth: process.env.DISABLE_EMAIL_ONLY_AUTH === 'true',
circleDomain: process.env.CIRCLE_COMMUNITY_DOMAIN,
appDomain: process.env.APP_DOMAIN,
environment: process.env.NODE_ENV || 'development'
});
});
// Security middleware to check if request is from iframe
function checkIframeEmbedding(req, res, next) {
// Only enforce in production with explicit flag
if (process.env.REQUIRE_IFRAME === 'true' && process.env.NODE_ENV === 'production') {
const referer = req.headers.referer || req.headers.referrer;
const origin = req.headers.origin;
const secFetchDest = req.headers['sec-fetch-dest'];
console.log('Security check - Referer:', referer);
console.log('Security check - Origin:', origin);
console.log('Security check - Sec-Fetch-Dest:', secFetchDest);
// Allow requests from our own app domain (iframe making requests to itself)
const appDomain = process.env.APP_DOMAIN;
const circleDomain = process.env.CIRCLE_COMMUNITY_DOMAIN;
// If referer is from our app domain, it's likely an iframe making API calls
if (referer && appDomain && referer.includes(appDomain)) {
console.log('Request from app domain (iframe API call) - allowing');
return next();
}
// Check if it's a direct browser navigation (not an API call from iframe)
if (secFetchDest === 'document' || secFetchDest === 'navigate') {
// This is someone trying to load the page directly
// Only allow if referer is from Circle domain
if (!referer || !circleDomain || !referer.includes(circleDomain)) {
console.error('Security: Direct navigation not from Circle domain');
return res.status(403).json({
error: 'Forbidden',
message: 'Direct access not allowed. Please access through Circle.'
});
}
}
// For API calls (empty or cors sec-fetch-dest), allow from allowed origins
// This handles the iframe making requests back to the server
console.log('API request - allowing');
}
next();
}
app.post('/api/auth', checkIframeEmbedding, async (req, res) => {
try {
const { email, community_member_id, sso_id, name, avatar_url } = req.body;
console.log('=== AUTH REQUEST START ===');
console.log('Request body:', JSON.stringify(req.body, null, 2));
console.log('Headers:', JSON.stringify(req.headers, null, 2));
// Security: Disable email-only authentication in production
if (process.env.DISABLE_EMAIL_ONLY_AUTH === 'true') {
// Require either a valid postMessage auth (with name) or member_id/sso_id
if (email && !name && !community_member_id && !sso_id) {
console.error('Security: Email-only authentication is disabled');
return res.status(403).json({
error: 'Authentication failed',
message: 'Email-only authentication is disabled for security. Please access this app through Circle.'
});
}
}
if (!process.env.CIRCLE_API_TOKEN) {
console.error('ERROR: Circle API token not configured');
return res.status(500).json({ error: 'Circle API token not configured' });
}
// Validate and clean authentication parameters
const authData = {};
// Check for template variables that weren't replaced
if (email) {
if (email.includes('{{') && email.includes('}}')) {
console.error('ERROR: Email contains unprocessed template variable:', email);
return res.status(400).json({
error: 'Invalid email format',
message: 'The email parameter contains an unprocessed template variable. Please provide a valid email address.',
details: 'Template variables like {{member.email}} should be replaced with actual values before accessing this endpoint.'
});
}
// Basic email validation
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
console.error('ERROR: Invalid email format:', email);
return res.status(400).json({
error: 'Invalid email format',
message: 'Please provide a valid email address.',
details: `Received email: ${email}`
});
}
authData.email = email;
}
if (community_member_id) {
if (community_member_id.includes('{{') && community_member_id.includes('}}')) {
console.error('ERROR: Community member ID contains unprocessed template variable:', community_member_id);
return res.status(400).json({
error: 'Invalid community member ID format',
message: 'The member ID parameter contains an unprocessed template variable. Please provide a valid member ID.',
details: 'Template variables should be replaced with actual values before accessing this endpoint.'
});
}
authData.community_member_id = community_member_id;
}
if (sso_id) {
if (sso_id.includes('{{') && sso_id.includes('}}')) {
console.error('ERROR: SSO ID contains unprocessed template variable:', sso_id);
return res.status(400).json({
error: 'Invalid SSO ID format',
message: 'The SSO ID parameter contains an unprocessed template variable. Please provide a valid SSO ID.',
details: 'Template variables should be replaced with actual values before accessing this endpoint.'
});
}
authData.sso_id = sso_id;
}
console.log('Auth data being sent to Circle API:', JSON.stringify(authData, null, 2));
console.log('Circle API URL:', 'https://app.circle.so/api/v1/headless/auth_token');
const authResponse = await axios.post(
'https://app.circle.so/api/v1/headless/auth_token',
authData,
{
headers: {
'Authorization': `Bearer ${process.env.CIRCLE_API_TOKEN}`,
'Content-Type': 'application/json'
}
}
);
console.log('Circle API auth response status:', authResponse.status);
console.log('Circle API auth response data:', JSON.stringify(authResponse.data, null, 2));
const { access_token, community_member_id: memberId } = authResponse.data;
if (!access_token) {
console.error('ERROR: No access_token received from Circle API');
throw new Error('No access token received from Circle API');
}
if (!memberId) {
console.error('ERROR: No community_member_id received from Circle API');
throw new Error('No community member ID received from Circle API');
}
console.log('Retrieved access_token (first 10 chars):', access_token.substring(0, 10) + '...');
console.log('Retrieved community_member_id:', memberId);
// Get member profile using the correct endpoint
let memberData = null;
try {
// Use the /community_member endpoint (correct one from API docs)
const memberUrl = `https://app.circle.so/api/headless/v1/community_member`;
console.log('Fetching member data from:', memberUrl);
const memberResponse = await axios.get(memberUrl, {
headers: {
'Authorization': `Bearer ${access_token}`,
'Content-Type': 'application/json'
}
});
console.log('Member API response status:', memberResponse.status);
console.log('Member API response data:', JSON.stringify(memberResponse.data, null, 2));
memberData = memberResponse.data;
// Extract useful fields for display
if (memberData) {
// Ensure we have all the fields we need
memberData.id = memberData.id || memberId;
memberData.community_member_id = memberId;
memberData.community_id = authResponse.data.community_id;
// Check for paid status via tags or other indicators
memberData.is_paid = false;
// Ensure tags is an array (Circle might return it as string or array)
// Check multiple possible fields where tags might be stored
const possibleTags = memberData.tags || memberData.labels || memberData.member_tags || [];
if (possibleTags) {
if (typeof possibleTags === 'string') {
// If tags is a comma-separated string, split it
memberData.tags = possibleTags.split(',').map(tag => tag.trim());
} else if (Array.isArray(possibleTags)) {
// If it's an array of objects with name/label properties
memberData.tags = possibleTags.map(tag => {
if (typeof tag === 'string') return tag;
if (tag.name) return tag.name;
if (tag.label) return tag.label;
if (tag.title) return tag.title;
return String(tag);
});
} else {
memberData.tags = [];
}
} else {
memberData.tags = [];
}
// Paid status detection - using tags ONLY
let isPaidMember = false;
// Check member tags against configured paid tags
if (memberData.tags && Array.isArray(memberData.tags) && memberData.tags.length > 0) {
isPaidMember = memberData.tags.some(tag =>
typeof tag === 'string' && paidMemberTags.some(paidTag => tag.toLowerCase().includes(paidTag))
);
}
memberData.is_paid = isPaidMember;
console.log('Paid status detection (tags only):', {
email: memberData.email,
tags: memberData.tags || [],
matched_paid_tag: isPaidMember,
configured_paid_tags: paidMemberTags
});
}
} catch (memberError) {
console.log('Community member endpoint failed. Trying public profile...');
try {
// Try public profile endpoint as fallback
const publicProfileUrl = `https://app.circle.so/api/headless/v1/community_members/${memberId}/public_profile`;
const profileResponse = await axios.get(publicProfileUrl, {
headers: {
'Authorization': `Bearer ${access_token}`,
'Content-Type': 'application/json'
}
});
console.log('Public profile API response status:', profileResponse.status);
console.log('Public profile API response data:', JSON.stringify(profileResponse.data, null, 2));
memberData = profileResponse.data;
} catch (profileError) {
console.log('All member endpoints failed. Using data from auth response...');
// If all endpoints fail, construct member data from what we have
memberData = {
id: memberId,
community_member_id: memberId,
community_id: authResponse.data.community_id,
email: email || authData.email,
name: req.body.name || 'Circle Member',
avatar_url: req.body.avatar_url || null,
is_paid: false,
tags: []
};
}
}
// Database integration: Upsert member data and handle credits
if (memberData && memberData.id && process.env.DATABASE_URL) {
try {
console.log('=== DATABASE INTEGRATION START ===');
console.log('Upserting member data into database...');
// First, check the member's CURRENT status in DB before updating
const existingMemberQuery = await pool.query(
'SELECT id, is_paid, tags FROM members WHERE circle_member_id = $1',
[memberData.id || memberData.community_member_id]
);
const existingMember = existingMemberQuery.rows[0];
const previousPaidStatus = existingMember?.is_paid || false;
const previousTags = existingMember?.tags || [];
// 1. UPSERT Member Data
const upsertQuery = `
INSERT INTO members (
circle_member_id, circle_user_id, email, name, avatar_url,
is_admin, is_moderator, is_paid, tags, last_seen_at
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, NOW())
ON CONFLICT (circle_member_id)
DO UPDATE SET
email = EXCLUDED.email,
name = EXCLUDED.name,
avatar_url = EXCLUDED.avatar_url,
is_admin = EXCLUDED.is_admin,
is_moderator = EXCLUDED.is_moderator,
is_paid = EXCLUDED.is_paid,
tags = EXCLUDED.tags,
last_seen_at = NOW(),
updated_at = NOW()
RETURNING id, first_seen_at, created_at;
`;
// Extract admin and moderator status from Circle's roles object
const isAdmin = memberData.roles?.admin || memberData.is_admin || false;
const isModerator = memberData.roles?.moderator || memberData.is_moderator || false;
console.log('Role detection:', {
'roles object': memberData.roles,
'extracted isAdmin': isAdmin,
'extracted isModerator': isModerator
});
// Determine paid status before upserting
const currentIsPaid = memberData.is_paid !== undefined ? memberData.is_paid :
(memberData.tags && memberData.tags.some(tag =>
typeof tag === 'string' && paidMemberTags.some(paidTag => tag.toLowerCase().includes(paidTag))
)) || false;
const memberResult = await pool.query(upsertQuery, [
memberData.id || memberData.community_member_id,
memberData.user_id,
memberData.email || email || authData.email,
memberData.name || req.body.name || 'Circle Member',
memberData.avatar_url || req.body.avatar_url,
isAdmin,
isModerator,
currentIsPaid,
JSON.stringify(memberData.tags || memberData.member_tags || [])
]);
const { id: dbMemberId, first_seen_at: firstSeenAt } = memberResult.rows[0];
// Check if this is a new user (created within the last 30 seconds)
const isNewUser = (new Date() - new Date(firstSeenAt)) < 30000;
console.log('Member upserted with DB ID:', dbMemberId);
console.log('Is new user:', isNewUser);
// Ensure paid status is always detected based on tags
// Update memberData.is_paid to match what was saved to database
memberData.is_paid = currentIsPaid;
if (memberData.tags) {
console.log('Paid status verification:', {
tags: memberData.tags,
is_paid: memberData.is_paid,
configured_paid_tags: paidMemberTags
});
}
// 2. Handle Credits (Initial Grant & Monthly Replenishment)
console.log('=== STARTING CREDIT PROCESSING ===');
let creditsResult = await pool.query(
'SELECT * FROM member_credits WHERE member_id = $1',
[dbMemberId]
);
console.log('Credits query result:', creditsResult.rows.length > 0 ? 'found' : 'not found');
if (creditsResult.rows.length === 0 || isNewUser) {
// New user or no credit record exists, grant initial credits
console.log(`New user or missing credits detected (DB ID: ${dbMemberId}). Granting initial credits.`);
const initialCredits = memberData.is_paid
? parseInt(process.env.INITIAL_CREDITS_PAID, 10) || 100
: parseInt(process.env.INITIAL_CREDITS_FREE, 10) || 10;
creditsResult = await pool.query(`
INSERT INTO member_credits (member_id, credits_balance, last_refreshed_at)
VALUES ($1, $2, NOW())
ON CONFLICT (member_id)
DO UPDATE SET
credits_balance = EXCLUDED.credits_balance,
last_refreshed_at = EXCLUDED.last_refreshed_at,
updated_at = NOW()
RETURNING *
`, [dbMemberId, initialCredits]);
// Log the initial credit grant
await pool.query(`
INSERT INTO credit_history (member_id, change_amount, change_type, balance_after, notes)
VALUES ($1, $2, $3, $4, $5)
`, [
dbMemberId,
initialCredits,
'initial_grant',
initialCredits,
`Initial credit grant: ${memberData.is_paid ? 'paid' : 'free'} member`
]);
} else {
// Returning user, check for monthly refresh AND status change
const creditsRecord = creditsResult.rows[0];
const lastRefresh = new Date(creditsRecord.last_refreshed_at);
const oneMonthAgo = new Date();
oneMonthAgo.setMonth(oneMonthAgo.getMonth() - 1);
// Check if member's paid status changed (using the status we captured BEFORE updating DB)
const tagsChanged = JSON.stringify(previousTags) !== JSON.stringify(memberData.tags || []);
if (previousPaidStatus !== memberData.is_paid || tagsChanged) {
console.log(`Member ${memberData.email} status/tags changed:`, {
previousPaidStatus,
currentPaidStatus: memberData.is_paid,
previousTags,
currentTags: memberData.tags
});
// If they upgraded to paid, give them additional credits
if (!previousPaidStatus && memberData.is_paid) {
const upgradeBonus = (parseInt(process.env.INITIAL_CREDITS_PAID, 10) || 100) -
(parseInt(process.env.INITIAL_CREDITS_FREE, 10) || 10);
const newBalance = creditsRecord.credits_balance + upgradeBonus;
creditsResult = await pool.query(`
UPDATE member_credits
SET credits_balance = $1, updated_at = NOW()
WHERE member_id = $2
RETURNING *
`, [newBalance, dbMemberId]);
// Log the upgrade bonus
await pool.query(`
INSERT INTO credit_history (member_id, change_amount, change_type, balance_after, notes)
VALUES ($1, $2, $3, $4, $5)
`, [
dbMemberId,
upgradeBonus,
'upgrade_bonus',
newBalance,
'Credit bonus for upgrading to paid membership'
]);
console.log(`Added ${upgradeBonus} upgrade bonus credits for ${memberData.email}`);
}
}
// Regular monthly refresh check
if (lastRefresh < oneMonthAgo) {
console.log(`User DB ID ${dbMemberId} is eligible for credit refresh.`);
const monthlyCredits = memberData.is_paid
? parseInt(process.env.MONTHLY_CREDITS_PAID, 10) || 100
: parseInt(process.env.MONTHLY_CREDITS_FREE, 10) || 10;
const currentBalance = creditsResult.rows[0]?.credits_balance || creditsRecord.credits_balance;
const newBalance = currentBalance + monthlyCredits;
creditsResult = await pool.query(`
UPDATE member_credits
SET credits_balance = $1, last_refreshed_at = NOW(), updated_at = NOW()
WHERE member_id = $2
RETURNING *
`, [newBalance, dbMemberId]);
// Log the monthly refresh
await pool.query(`
INSERT INTO credit_history (member_id, change_amount, change_type, balance_after, notes)
VALUES ($1, $2, $3, $4, $5)
`, [
dbMemberId,
monthlyCredits,
'monthly_refresh',
newBalance,
`Monthly credit refresh: ${memberData.is_paid ? 'paid' : 'free'} member`
]);
}
}
console.log('=== ABOUT TO PROCESS PURCHASE TAGS ===');
console.log('Current execution point reached');
try {
// 3. Process One-Time Purchase Tags
// Check for numeric tags (e.g., "$10", "$50", "$100", "10", "50", "100")
const purchaseTags = [];
const processedTags = [];
console.log('=== PURCHASE TAG DETECTION START ===');
console.log('memberData.tags:', memberData.tags);
console.log('Is array?', Array.isArray(memberData.tags));
if (memberData.tags && Array.isArray(memberData.tags)) {
for (const tag of memberData.tags) {
// Match tags like "$10", "$50", "$100" or just "10", "50", "100"
const tagStr = String(tag).trim();
const purchaseMatch = tagStr.match(/^\$?(\d+)$/);
console.log(`Checking tag "${tagStr}":`, { purchaseMatch });
if (purchaseMatch) {
const creditAmount = parseInt(purchaseMatch[1], 10);
// Only process reasonable credit amounts (1-10000)
if (creditAmount > 0 && creditAmount <= 10000) {
console.log(`Valid purchase tag found: "${tagStr}" = ${creditAmount} credits`);
purchaseTags.push({
tag: tagStr,
credits: creditAmount
});
}
}
}
}
console.log('Purchase tags found:', purchaseTags);
console.log('=== PURCHASE TAG DETECTION END ===');
// Process purchase tags immediately (no time limit)
if (purchaseTags.length > 0) {
console.log(`Found purchase tags for ${memberData.email}:`, purchaseTags);
let totalCreditsToAdd = 0;
const tagsToProcess = [];
const tagsToDelete = [];
// Process all numeric tags immediately and prepare for deletion
for (const purchase of purchaseTags) {
totalCreditsToAdd += purchase.credits;
tagsToProcess.push(purchase);
// Get the admin tag ID for deletion
const adminTagId = await getAdminTagIdByName(purchase.tag);
if (adminTagId) {
tagsToDelete.push({
id: adminTagId,
name: purchase.tag,
credits: purchase.credits
});
console.log(`Will process and delete purchase tag "${purchase.tag}" (admin_id: ${adminTagId}) for ${purchase.credits} credits`);
} else {
console.log(`Will process purchase tag "${purchase.tag}" for ${purchase.credits} credits (couldn't find admin tag ID for deletion)`);
}
}
// If there are any new credits to add, perform one transaction
if (totalCreditsToAdd > 0) {
console.log(`Processing ${tagsToProcess.length} new purchase tags for a total of ${totalCreditsToAdd} credits.`);
await pool.query('BEGIN');
try {
const currentBalance = creditsResult.rows[0].credits_balance;
const newBalance = currentBalance + totalCreditsToAdd;
// 1. Update the balance once
await pool.query(`
UPDATE member_credits
SET credits_balance = $1, updated_at = NOW()
WHERE member_id = $2
`, [newBalance, dbMemberId]);
// 2. Log all processed tags and history records
for (const purchase of tagsToProcess) {
// Log the processed tag for audit purposes (no longer prevents reprocessing)
await pool.query(`
INSERT INTO processed_purchase_tags (member_id, tag_value, credits_granted)
VALUES ($1, $2, $3)
`, [dbMemberId, purchase.tag, purchase.credits]);
await pool.query(`
INSERT INTO credit_history (member_id, change_amount, change_type, balance_after, notes)
VALUES ($1, $2, $3, $4, $5)
`, [
dbMemberId,
purchase.credits,
'purchase',
newBalance,
`One-time purchase: ${purchase.tag}`
]);
}
await pool.query('COMMIT');
// Update local state for the response
creditsResult.rows[0].credits_balance = newBalance;
processedTags.push(...tagsToProcess.map(p => p.tag));
memberData.processed_purchase_tags = processedTags;
console.log(`Successfully granted a total of ${totalCreditsToAdd} credits from tags:`, processedTags);
// Delete the tags from Circle after successful credit processing
if (tagsToDelete.length > 0) {
console.log(`Deleting ${tagsToDelete.length} purchase tags from Circle...`);
for (const tagInfo of tagsToDelete) {
const deleteSuccess = await deleteMemberTag(memberData.email, tagInfo.id);
if (deleteSuccess) {
console.log(`Deleted tag "${tagInfo.name}" (${tagInfo.credits} credits) from Circle`);
} else {
console.log(`Failed to delete tag "${tagInfo.name}" from Circle, but credits were already added`);
}
}
}
} catch (txError) {
await pool.query('ROLLBACK');
console.error(`Failed to process batch of purchase tags:`, txError);
}
}
}
} catch (purchaseTagError) {
console.error('=== PURCHASE TAG PROCESSING ERROR ===');
console.error('Error processing purchase tags:', purchaseTagError);
console.error('Stack trace:', purchaseTagError.stack);
}
// 4. Enhance memberData with credits and database info
const finalCreditsData = creditsResult.rows[0];
memberData.db_id = dbMemberId;
memberData.credits_balance = finalCreditsData.credits_balance;
memberData.credits_last_refreshed = finalCreditsData.last_refreshed_at;
memberData.is_new_user = isNewUser;
// Ensure admin/moderator status is available for frontend
memberData.is_admin = isAdmin;
memberData.is_moderator = isModerator;
console.log('Final member data with credits:', {
db_id: memberData.db_id,
credits_balance: memberData.credits_balance,
is_new_user: memberData.is_new_user,
is_admin: memberData.is_admin,
is_moderator: memberData.is_moderator
});
console.log('=== DATABASE INTEGRATION SUCCESS ===');
} catch (dbError) {
console.error('=== DATABASE ERROR ===');
console.error('Database operation failed:', dbError.message);
console.error('Full error:', dbError);
// Don't fail the auth request, just log the error
// The user can still authenticate even if DB operations fail
memberData.db_error = 'Database operations failed, but authentication succeeded';
memberData.credits_balance = 0; // Fallback
}
} else if (!process.env.DATABASE_URL) {
console.log('Database URL not configured, skipping database operations');
memberData.credits_balance = 0; // Fallback for no DB
}
console.log('=== AUTH REQUEST SUCCESS ===');
res.json({
success: true,
member: memberData,
access_token: access_token,
refresh_token: authResponse.data.refresh_token,
expires_at: authResponse.data.access_token_expires_at
});
} catch (error) {
console.error('=== AUTH ERROR START ===');
console.error('Error type:', error.constructor.name);
console.error('Error message:', error.message);
if (error.response) {
console.error('HTTP Error Response:');
console.error(' Status:', error.response.status);
console.error(' Status Text:', error.response.statusText);
console.error(' Headers:', JSON.stringify(error.response.headers, null, 2));
console.error(' Data:', JSON.stringify(error.response.data, null, 2));
}
if (error.request) {
console.error('Request details:');
console.error(' Method:', error.request.method);
console.error(' URL:', error.request.url || error.request.path);
console.error(' Headers:', JSON.stringify(error.request.getHeaders ? error.request.getHeaders() : 'N/A', null, 2));
}
console.error('Full error object:', JSON.stringify(error, Object.getOwnPropertyNames(error), 2));
console.error('=== AUTH ERROR END ===');
res.status(error.response?.status || 500).json({
error: 'Authentication failed',
details: error.response?.data || error.message,
debug_info: {
error_type: error.constructor.name,
has_response: !!error.response,
has_request: !!error.request,
response_status: error.response?.status,
response_data: error.response?.data
}
});
}
});
// New endpoint for cookie-based authentication (for iframe embedding)
app.post('/api/auth/cookies', async (req, res) => {
try {
const { access_token, redirect_to } = req.body;
console.log('=== COOKIE AUTH REQUEST START ===');
console.log('Access token provided:', access_token ? 'Yes (length: ' + access_token.length + ')' : 'No');
console.log('Redirect to:', redirect_to);
if (!access_token) {
console.error('ERROR: No access token provided');
return res.status(400).json({
error: 'No access token provided',
message: 'An access token is required for cookie-based authentication'
});
}
// Use Circle's cookies API to set session cookies
const communityDomain = process.env.CIRCLE_COMMUNITY_DOMAIN || 'community.circle.so';
const cookiesUrl = `https://${communityDomain}/api/headless/v1/cookies`;
console.log('Setting cookies via:', cookiesUrl);
const cookieResponse = await axios.post(cookiesUrl, {}, {
headers: {
'Authorization': `Bearer ${access_token}`,
'Content-Type': 'application/json'
}
});
console.log('Cookie response status:', cookieResponse.status);
console.log('Cookie response:', JSON.stringify(cookieResponse.data, null, 2));
console.log('=== COOKIE AUTH SUCCESS ===');
res.json({
success: true,
message: 'Session cookies set successfully',
redirect_to: redirect_to || '/'
});
} catch (error) {
console.error('=== COOKIE AUTH ERROR START ===');
console.error('Error type:', error.constructor.name);
console.error('Error message:', error.message);
if (error.response) {
console.error('HTTP Error Response:');
console.error(' Status:', error.response.status);
console.error(' Data:', JSON.stringify(error.response.data, null, 2));
}
console.error('=== COOKIE AUTH ERROR END ===');
res.status(error.response?.status || 500).json({
error: 'Cookie authentication failed',
details: error.response?.data || error.message
});
}
});
// Generate secure admin token for popup window access
app.post('/api/admin/generate-token', async (req, res) => {
try {
const { circle_member_id } = req.body;
if (!circle_member_id) {
return res.status(400).json({
error: 'Missing circle_member_id',
message: 'Member ID required for admin token generation'
});
}
if (!process.env.DATABASE_URL) {
return res.status(501).json({
error: 'Database not configured',
message: 'Admin features require database configuration'
});
}
// Verify the user is actually an admin in our database
const result = await pool.query(`
SELECT is_admin, name, email
FROM members
WHERE circle_member_id = $1 AND is_admin = true
`, [circle_member_id]);
if (result.rows.length === 0) {
console.warn(`Admin token generation denied for member ${circle_member_id} - not an admin`);
return res.status(403).json({
error: 'Access denied',
message: 'Admin privileges required'
});
}
// Generate secure random token
const token = crypto.randomBytes(32).toString('hex');
const expiresAt = new Date(Date.now() + 15 * 60 * 1000); // 15 minutes
// Store token with admin info
adminTokens.set(token, {
circle_member_id,
name: result.rows[0].name,
email: result.rows[0].email,
expiresAt,
createdAt: new Date()
});
console.log(`Admin token generated for ${result.rows[0].name} (${result.rows[0].email}), expires at ${expiresAt.toISOString()}`);
// Clean up expired tokens periodically
cleanupExpiredTokens();
res.json({
success: true,
token,
expiresAt: expiresAt.toISOString(),
adminUrl: `/admin.html?token=${token}`
});
} catch (error) {
console.error('Generate admin token error:', error.message);
res.status(500).json({
error: 'Failed to generate admin token',
details: error.message
});
}
});
// Helper function to clean up expired tokens
function cleanupExpiredTokens() {
const now = new Date();
for (const [token, data] of adminTokens.entries()) {
if (data.expiresAt < now) {
adminTokens.delete(token);
}
}
}
// Check if user has valid Circle session cookies
app.get('/api/member/check', async (req, res) => {
try {
console.log('=== MEMBER SESSION CHECK START ===');
console.log('Cookies:', req.headers.cookie ? 'Present' : 'None');
// Look for Circle-specific cookies
const cookies = req.headers.cookie || '';