Skip to content

Commit

Permalink
feat: create the signin method
Browse files Browse the repository at this point in the history
  • Loading branch information
FREDVUNI committed Oct 26, 2023
1 parent a01aa21 commit 8a5031a
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions controllers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ const createUser = async (req, res) => {
try {
const { username, password } = req.body;

const salt = bcrypt.genSaltSync(10);
const hashPassword = bcrypt.hashSync(password,salt)
const salt = bcrypt.genSaltSync(10);
const hashPassword = bcrypt.hashSync(password, salt);

const newUser = await prisma.user.create({
data: { username, password:hashPassword },
data: { username, password: hashPassword },
});
res.status(200).json({
message: "User has been added.",
Expand All @@ -31,6 +31,33 @@ const createUser = async (req, res) => {
}
};

const signIn = async (req, res) => {
try {
const { username, password } = req.body;
const user = await prisma.user.findUnique({
where: {
username: username,
},
});
if (!user)
return res.status(404).json({
message: "wrong email password combination",
});
const user_password = bcrypt.compareSync(password,user.password);
if (!user_password)
return res.status(404).json({
message: "wrong email password combination",
});

res.status(200).json({
message: "User has been logged in.",
token: JWT.sign({ user: user }, process.env.SECRET_KEY),
});
} catch (error) {
res.status(500).json(error.message || "There was a server error.");
}
};

const deleteUser = async (req, res) => {
try {
await prisma.user.delete({
Expand All @@ -46,4 +73,4 @@ const deleteUser = async (req, res) => {
}
};

module.exports = { getUsers, createUser, deleteUser };
module.exports = { getUsers, createUser, deleteUser, signIn };

0 comments on commit 8a5031a

Please sign in to comment.