-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.js
32 lines (28 loc) · 1.07 KB
/
users.js
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
const express = require('express');
const router = express.Router();
const users = []; // This should be replaced with a database in a real application
// Register new user
router.post('/users/register', (req, res) => {
const { username, password } = req.body;
if (!username || !password) {
return res.status(400).send('Username and password are required');
}
const userExists = users.find(u => u.username === username);
if (userExists) {
return res.status(400).send('User already exists');
}
const newUser = { id: users.length + 1, username, password }; // Password should be hashed
users.push(newUser);
res.status(201).json(newUser);
});
// Login user
router.post('/users/login', (req, res) => {
const { username, password } = req.body;
const user = users.find(u => u.username === username && u.password === password);
if (user) {
res.json({ message: 'Login successful', user });
} else {
res.status(401).send('Invalid username or password');
}
});
module.exports = router;