-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-service-role-signup.js
More file actions
93 lines (75 loc) · 2.66 KB
/
Copy pathtest-service-role-signup.js
File metadata and controls
93 lines (75 loc) · 2.66 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
const { createClient } = require('@supabase/supabase-js')
async function testServiceRoleSignup() {
console.log('🧪 Testing user creation with service role...')
try {
const supabaseUrl = 'https://rfakvkihqdhfueclbkhm.supabase.co'
const serviceRoleKey =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InJmYWt2a2locWRoZnVlY2xia2htIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTc1MTY4NTYyNiwiZXhwIjoyMDY3MjYxNjI2fQ.SXpHrTBNYk3mX7SHveBc76hwXSmQY10dmGrn1F3Gor4'
const supabase = createClient(supabaseUrl, serviceRoleKey, {
auth: {
autoRefreshToken: false,
persistSession: false,
},
})
const testEmail = `test-service-${Date.now()}@example.com`
console.log('📧 Creating user with admin.createUser...')
// Use admin.createUser instead of auth.signUp
const { data: userData, error: userError } =
await supabase.auth.admin.createUser({
email: testEmail,
password: 'TestPassword123!',
email_confirm: true, // Auto-confirm email
user_metadata: {
first_name: 'Test',
last_name: 'User',
full_name: 'Test User',
},
})
if (userError) {
console.log('❌ User creation failed:', userError.message)
return
}
console.log('✅ User created successfully:', userData.user.id)
// Now test organization creation
console.log('🏢 Creating organization...')
const organizationName = 'Test Company Admin'
const slug = organizationName
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '')
.substring(0, 50)
const uniqueSlug = `${slug}-${Date.now()}`
// Create organization
const { data: org, error: orgError } = await supabase
.from('organizations')
.insert({
name: organizationName,
slug: uniqueSlug,
created_by: userData.user.id,
})
.select()
.single()
if (orgError) {
console.log('❌ Organization creation failed:', orgError.message)
return
}
console.log('✅ Organization created:', org.id)
// Create membership
const { error: membershipError } = await supabase
.from('organization_members')
.insert({
user_id: userData.user.id,
organization_id: org.id,
role: 'owner',
})
if (membershipError) {
console.log('❌ Membership creation failed:', membershipError.message)
return
}
console.log('✅ Membership created successfully')
console.log('🎉 Service role user + organization creation working!')
} catch (error) {
console.log('❌ Unexpected error:', error.message)
}
}
testServiceRoleSignup()