mirror-filecoffee-filehost/middlewares/auth.middleware.js
2024-06-09 15:16:29 +02:00

16 lines
453 B
JavaScript

const bcrypt = require("bcryptjs");
const User = require("../database/models/user.model");
const authenticate = async (req, res, next) => {
const { username, password } = req.body;
const user = await User.findOne({ where: { username } });
if (user && bcrypt.compareSync(password, user.password)) {
req.session.userId = user.id;
next();
} else {
res.status(401).send("Authentication failed");
}
};
module.exports = authenticate;