This commit is contained in:
BIG2EYEZ 2023-12-21 00:55:39 +08:00
parent 30336bcb28
commit 694e76951d
4 changed files with 33 additions and 14 deletions

View File

@ -39,7 +39,7 @@ const transporter = nodemailer.createTransport({
pass: process.env.epass pass: process.env.epass
}, },
}); });
console.log(process.env.euser);
app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.urlencoded({ extended: true }));
app.use(session({ secret: 'your_session_secret', resave: false, saveUninitialized: true })); app.use(session({ secret: 'your_session_secret', resave: false, saveUninitialized: true }));
app.set('view engine', 'ejs'); app.set('view engine', 'ejs');
@ -332,6 +332,7 @@ app.post('/forgot-password', (req, res) => {
}); });
}); });
// Handle Reset Password request // Handle Reset Password request
// Handle Reset Password request
app.post('/reset-password/:token', async (req, res) => { app.post('/reset-password/:token', async (req, res) => {
const { token } = req.params; const { token } = req.params;
const { password, confirmPassword } = req.body; const { password, confirmPassword } = req.body;
@ -345,17 +346,20 @@ app.post('/reset-password/:token', async (req, res) => {
} }
if (selectResults.length === 0) { if (selectResults.length === 0) {
return res.status(400).json({ error: 'Invalid or expired reset token' }); // Pass the error to the template when rendering the reset-password page
return res.render('reset-password', { token, resetError: 'Invalid or expired reset token' });
} }
// Check if passwords match // Check if passwords match
if (password !== confirmPassword) { if (password !== confirmPassword) {
return res.render('reset-password', { token, error: 'Passwords do not match' }); // Pass the error to the template when rendering the reset-password page
return res.render('reset-password', { token, resetError: 'Passwords do not match' });
} }
// Check if the new password meets complexity requirements // Check if the new password meets complexity requirements
if (!isStrongPassword(password)) { if (!isStrongPassword(password)) {
return res.render('reset-password', { token, error: 'Password does not meet complexity requirements. It must be at least 10 characters long and include at least one uppercase letter, one lowercase letter, one digit, and one symbol.' }); // Pass the error to the template when rendering the reset-password page
return res.render('reset-password', { token, resetError: 'Password does not meet complexity requirements. It must be at least 10 characters long and include at least one uppercase letter, one lowercase letter, one digit, and one symbol.' });
} }
// Hash the new password // Hash the new password
@ -366,18 +370,24 @@ app.post('/reset-password/:token', async (req, res) => {
mysqlConnection.query(updateQuery, [hashedPassword, token], (updateErr) => { mysqlConnection.query(updateQuery, [hashedPassword, token], (updateErr) => {
if (updateErr) { if (updateErr) {
console.error('Error updating password:', updateErr); console.error('Error updating password:', updateErr);
res.status(500).json({ error: 'Error updating password' }); // Pass the error to the template when rendering the reset-password page
res.render('reset-password', { token, resetError: 'Error updating password' });
} else { } else {
res.render('reset-password', { error: null, success: 'Password changed successfully', token }); // Pass the success message to the template when rendering the reset-password page
res.render('reset-password', { token, resetError: null, success: 'Password changed successfully' });
} }
}); });
}); });
}); });
app.get('/reset-password/:token', (req, res) => { app.get('/reset-password/:token', (req, res) => {
const { token } = req.params; const { token } = req.params;
const error = req.query.error || null; // Get error from query parameter const error = req.query.error || null; // Get error from query parameter
res.render('reset-password', { token, error: null, success: null }); // Assuming you have this line in your server code where you render the reset-password view
res.render('reset-password', { token, passwordValidationError: null, resetError: null, success: null });
}); });
app.use(express.static('views')); app.use(express.static('views'));

View File

@ -76,8 +76,8 @@
<% } %> <% } %>
<div class="input-box"> <div class="input-box">
<span class="details">Username or Email</span> <span class="details">Username</span>
<input type="text" name="usernameOrEmail" placeholder="Enter your username or email" required> <input type="text" name="usernameOrEmail" placeholder="Enter your email" required>
</div> </div>
<div class="button"> <div class="button">

View File

@ -254,6 +254,7 @@ document.getElementById('userForm').addEventListener('submit', function (event)
console.error('No data available for download.'); console.error('No data available for download.');
} }
} }
</script> </script>
</div> </div>

View File

@ -10,13 +10,15 @@
</head> </head>
<body> <body>
<div class="container mt-5"> <div class="container mt-5">
<% if (error) { %> <% if (resetError) { %>
<div class="alert alert-danger"><%= error %></div> <div class="alert alert-danger mb-3"><%= resetError %></div>
<% } else if (success) { %> <% } else if (success) { %>
<div class="alert alert-success"><%= success %></div> <div class="alert alert-success mb-3"><%= success %></div>
<p>Password changed successfully. <a href="/login">Click here to log in</a>.</p> <p>Password changed successfully. <a href="/login">Click here to log in</a>.</p>
<% } else { %> <% } else { %>
<h2 class="mb-4">Reset Your Password</h2> <% if (passwordValidationError) { %>
<div class="alert alert-danger mb-3"><%= passwordValidationError %></div>
<% } %>
<form action="/reset-password/<%= token %>" method="post"> <form action="/reset-password/<%= token %>" method="post">
<div class="form-group"> <div class="form-group">
<label for="password">New Password:</label> <label for="password">New Password:</label>
@ -32,3 +34,9 @@
</div> </div>
</body> </body>
</html> </html>