LOGIN ROUTE
LOGIN ROUTE
IN USERCONTROLLER.JS
module.exports.login = async (req, res) => {
const { username, password } = req.body;
if (!username || !password) {
return res.status(status.BAD_REQUEST).json({ message: "Fill all the fields!" });
}
try {
const foundUser = await User.findOne({ username });
console.log(foundUser);
if (!foundUser) {
return res.status(status.NOT_FOUND).json({ message: "User not Found!" });
}
const auth = await bcrypt.compare(password, foundUser.password);
if (!auth) {
return res.status(401).json({ message: "Invalid Password" });
}
const token = createSecretToken(foundUser._id);
res.cookie("token", token, {
withCredentials: true,
httpOnly: true,
});
return res.status(201).json({ message: "User LoggedIn Successfully!" });
} catch (e) {
return res.status(404).json({ message: `something went wrong ${e}` });
}
};
USERROUTER.JS
const router = require("express").Router();
const { signup, login } = require("../controllers/userController");
const { userVerification } = require("../Middlewares/Authentication");
router.post("/", userVerification);
router.post("/signup", signup);
router.post("/login", login);
module.exports = router;



Comments
Post a Comment