-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-reset-token.js
More file actions
93 lines (80 loc) · 3.04 KB
/
create-reset-token.js
File metadata and controls
93 lines (80 loc) · 3.04 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
// Script to generate a password reset token for a user
const { createClient } = require('@supabase/supabase-js');
require('dotenv').config({ path: '.env.local' });
// Get email from command line argument
const email = process.argv[2];
if (!email) {
console.error('Please provide an email address as argument');
process.exit(1);
}
// Use local Supabase URL and service role key
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL || 'http://127.0.0.1:54321';
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY || 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImV4cCI6MTk4MzgxMjk5Nn0.EGIM96RAZx35lJzdJsyH-qQwv8Hdp7fsn3W0YpN81IU';
// Get redirect URL
const redirectUrl = process.argv[3] || 'http://localhost:3000/reset-password';
// Create the Supabase client with service role key
const adminClient = createClient(supabaseUrl, serviceRoleKey, {
auth: {
autoRefreshToken: false,
persistSession: false,
},
});
// Also create a regular client
const client = createClient(supabaseUrl, process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY, {
auth: {
autoRefreshToken: false,
persistSession: false,
},
});
async function generateResetToken() {
try {
console.log(`Generating reset token for: ${email}`);
// Try approach 1: Use admin generate link
try {
console.log('Approach 1: Using admin.generateLink');
const { data, error } = await adminClient.auth.admin.generateLink({
type: 'recovery',
email,
options: {
redirectTo: redirectUrl,
}
});
if (error) {
console.error('Error with admin.generateLink:', error);
} else if (data && data.properties && data.properties.action_link) {
console.log('Success! Reset link generated:');
console.log(data.properties.action_link);
return;
}
} catch (err) {
console.error('Exception in admin.generateLink:', err);
}
// Try approach 2: Use regular resetPasswordForEmail
try {
console.log('\nApproach 2: Using resetPasswordForEmail');
const { data, error } = await client.auth.resetPasswordForEmail(email, {
redirectTo: redirectUrl,
});
if (error) {
console.error('Error with resetPasswordForEmail:', error);
} else {
console.log('Success! Reset email should be sent via Supabase');
console.log('Check logs for the reset URL');
}
} catch (err) {
console.error('Exception in resetPasswordForEmail:', err);
}
// Try approach 3: Direct SQL to get the user ID
try {
console.log('\nApproach 3: Direct SQL check');
// We'll just use this to confirm the user exists
console.log('Run this command to check the user:');
console.log(`docker exec supabase_db_propbot psql -U postgres -c "SELECT id, email FROM auth.users WHERE email = '${email}'"`);
} catch (err) {
console.error('Exception in SQL check:', err);
}
} catch (error) {
console.error('Error:', error);
}
}
generateResetToken();