-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cjs
More file actions
30 lines (23 loc) · 783 Bytes
/
server.cjs
File metadata and controls
30 lines (23 loc) · 783 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
import jsonServer from "json-server";
import jwt from "jsonwebtoken";
const server = jsonServer.create();
const router = jsonServer.router("db.json");
const middlewares = jsonServer.defaults();
server.use(middlewares);
server.use(jsonServer.bodyParser);
const SECRET_KEY = "your_secret_key";
const expiresIn = "1h";
server.post("/login", (req, res) => {
const { username, password } = req.body;
const user = router.db.get("users").find({ username, password }).value();
if (user) {
const token = jwt.sign({ username }, SECRET_KEY, { expiresIn });
res.status(200).json({ token });
} else {
res.status(401).json({ message: "Invalid username or password" });
}
});
server.use(router);
server.listen(5000, () => {
console.log("JSON Server is running");
});