-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangePassword.js
More file actions
36 lines (29 loc) · 898 Bytes
/
changePassword.js
File metadata and controls
36 lines (29 loc) · 898 Bytes
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
const express = require('express');
const db = require('./db.js');
const app = express();
const router = express.Router();
const port = 3000;
app.use(express.json());
router.put('/:user_id', (req, res) => {
const user_id = req.params.user_id;
const { password } = req.body;
const query = `
UPDATE users
SET
password = ?
WHERE user_id = ?
`;
const values = [password, user_id];
db.query(query, values, (err, result) => {
if (err) {
console.error('Error updating password: ' + err.stack);
res.status(500).json({ error: 'Internal server error' });
return;
}
if (result.affectedRows === 0) {
res.status(404).json({ error: 'User not found' });
return;
}
res.status(200).json({ message: 'Password updated successfully' });
});
});