implement user authentication with login functionality and database integration
This commit is contained in:
26
backend/routes/api.js
Normal file
26
backend/routes/api.js
Normal file
@@ -0,0 +1,26 @@
|
||||
import express from "express";
|
||||
import { loginFunc, getItemsFromDatabase } from "../services/database.js";
|
||||
import { authenticate, generateToken } from "../services/tokenService.js";
|
||||
const router = express.Router();
|
||||
|
||||
// Example endpoint
|
||||
router.post("/login", async (req, res) => {
|
||||
const result = await loginFunc(req.body.username, req.body.password);
|
||||
if (result.success) {
|
||||
const token = await generateToken({ username: req.body.username });
|
||||
res.status(200).json({ message: "Login successful", token });
|
||||
} else {
|
||||
res.status(401).json({ message: "Invalid credentials" });
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/items", authenticate, async (req, res) => {
|
||||
const result = await getItemsFromDatabase();
|
||||
if (result.success) {
|
||||
res.status(200).json(result.data);
|
||||
} else {
|
||||
res.status(500).json({ message: "Failed to fetch items" });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
Reference in New Issue
Block a user