Implement Auth

This commit is contained in:
Takeshi Kimata
2024-05-17 23:30:13 +09:00
parent bda47864f9
commit 8103297dae
16 changed files with 902 additions and 72 deletions

View File

@@ -0,0 +1,36 @@
const express = require("express");
const router = express.Router();
const defineUser = require("../../models/users");
const { DataTypes } = require("sequelize");
module.exports = function (sequelize) {
const User = defineUser(sequelize, DataTypes);
router.post("/auth/signin", async (req, res) => {
try {
const { email, password } = req.body;
const user = await User.findOne({
where: {
email: email,
},
});
if (!user) {
return res.status(401).json({ error: "Authentication failed" });
}
const passwordMatch = await bcrypt.compare(password, user.password);
if (!passwordMatch) {
return res.status(401).json({ error: "Authentication failed" });
}
const token = jwt.sign({ userId: user.id }, "your-secret-key", {
expiresIn: "1h",
});
res.status(200).json({ token });
} catch (error) {
console.error(error);
res.status(500).send("Sign up failed");
}
});
return router;
};