-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset-all-passwords.js
More file actions
46 lines (36 loc) · 1.24 KB
/
reset-all-passwords.js
File metadata and controls
46 lines (36 loc) · 1.24 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
import { scrypt, randomBytes } from 'crypto';
import { promisify } from 'util';
import pkg from 'pg';
const { Client } = pkg;
const scryptAsync = promisify(scrypt);
async function hashPassword(password) {
const salt = randomBytes(16).toString('hex');
const buf = await scryptAsync(password, salt, 64);
return `${buf.toString('hex')}.${salt}`;
}
async function resetAllPasswords() {
const client = new Client({
connectionString: process.env.DATABASE_URL
});
try {
await client.connect();
// Hash the new password
const hashedPassword = await hashPassword('thankyou');
console.log('New hashed password:', hashedPassword);
// Update ALL user passwords in the database
const result = await client.query(
'UPDATE users SET password = $1',
[hashedPassword]
);
console.log(`All user passwords have been updated to "thankyou". Rows affected: ${result.rowCount}`);
// List all users for verification
const { rows } = await client.query('SELECT id, username, email, role FROM users ORDER BY id');
console.log('\nUser accounts:');
console.table(rows);
} catch (error) {
console.error('Error:', error);
} finally {
await client.end();
}
}
resetAllPasswords();