-
Notifications
You must be signed in to change notification settings - Fork 267
Expand file tree
/
Copy pathreset-password.js
More file actions
77 lines (77 loc) · 2.82 KB
/
reset-password.js
File metadata and controls
77 lines (77 loc) · 2.82 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
const readline = require('readline');
const bcrypt = require('bcrypt');
const { db } = require('./db/database');
const User = require('./models/User');
require('dotenv').config();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function validatePassword(password) {
const minLength = password.length >= 8;
const hasLowercase = /[a-z]/.test(password);
const hasUppercase = /[A-Z]/.test(password);
const hasNumber = /[0-9]/.test(password);
const isValid = minLength && hasLowercase && hasUppercase && hasNumber;
if (!isValid) {
console.log('\nPassword requirements:');
if (!minLength) console.log('- Must be at least 8 characters long');
if (!hasLowercase) console.log('- Must contain at least one lowercase letter');
if (!hasUppercase) console.log('- Must contain at least one uppercase letter');
if (!hasNumber) console.log('- Must contain at least one number');
console.log('');
}
return isValid;
}
function askUsername() {
console.log('\n===== StreamFlow Lite - Password Reset =====\n');
rl.question('Enter username: ', async (username) => {
try {
const user = await User.findByUsername(username);
if (!user) {
console.log('\n❌ User not found! Please check the username and try again.');
askUsername();
return;
}
console.log(`\n✅ User found: ${username}`);
askNewPassword(user);
} catch (error) {
console.error('\n❌ Error finding user:', error);
askUsername();
}
});
}
function askNewPassword(user) {
rl.question('Enter new password: ', (password) => {
if (!validatePassword(password)) {
console.log('❌ Password does not meet requirements. Please try again.');
askNewPassword(user);
return;
}
askConfirmPassword(user, password);
});
}
function askConfirmPassword(user, password) {
rl.question('Confirm new password: ', async (confirmPassword) => {
if (password !== confirmPassword) {
console.log('\n❌ Passwords do not match! Please try again.');
askConfirmPassword(user, password);
return;
}
try {
const hashedPassword = await bcrypt.hash(password, 10);
await User.update(user.id, { password: hashedPassword });
console.log('\n✅ Password has been reset successfully!\n');
rl.close();
} catch (error) {
console.error('\n❌ Error resetting password:', error);
console.log('Please try again.');
askNewPassword(user);
}
});
}
askUsername();
rl.on('close', () => {
console.log('\nPassword reset utility closed.');
process.exit(0);
});