implement admin panel with login functionality and dashboard layout
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
getBorrowableItemsFromDatabase,
|
||||
createLoanInDatabase,
|
||||
onTake,
|
||||
loginAdmin,
|
||||
onReturn,
|
||||
} from "../services/database.js";
|
||||
import { authenticate, generateToken } from "../services/tokenService.js";
|
||||
@@ -166,4 +167,33 @@ router.post("/createLoan", authenticate, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
// Admin panel functions
|
||||
|
||||
router.post("/loginAdmin", async (req, res) => {
|
||||
const { username, password } = req.body || {};
|
||||
if (!username || !password) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ message: "Username and password are required" });
|
||||
}
|
||||
|
||||
const result = await loginAdmin(username, password);
|
||||
if (result.success) {
|
||||
const token = await generateToken({
|
||||
username: result.data.username,
|
||||
role: result.data.role,
|
||||
});
|
||||
|
||||
return res.status(200).json({
|
||||
message: "Login successful",
|
||||
first_name: result.data.first_name,
|
||||
token,
|
||||
});
|
||||
}
|
||||
|
||||
return res.status(401).json({ message: "Invalid credentials" });
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
Reference in New Issue
Block a user