114 lines
2.9 KiB
114 lines
2.9 KiB
import Users from "../models/userModel.js";
|
|
import bcrypt from "bcrypt";
|
|
import jwt from "jsonwebtoken";
|
|
|
|
//Get List User from database
|
|
|
|
export const getUsers = async (req, res) => {
|
|
try {
|
|
const users = await Users.findAll({
|
|
attributes: ["id", "name", "email"],
|
|
});
|
|
res.json(users);
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
};
|
|
|
|
export const Register = async (req, res) => {
|
|
const { name, email, password, confirmPassword } = req.body;
|
|
if (password !== confirmPassword)
|
|
return res.status(400).json({ msg: "Password don't match" });
|
|
|
|
// Check if name or email already exists
|
|
const existingUser = await Users.findOne({ where: { email: email } });
|
|
if (existingUser) {
|
|
return res.status(400).json({ msg: "Email already exists" });
|
|
}
|
|
|
|
//Hash Password with bcrypt with Register account
|
|
const salt = await bcrypt.genSalt();
|
|
const hashPassword = await bcrypt.hash(password, salt);
|
|
try {
|
|
await Users.create({
|
|
name: name,
|
|
email: email,
|
|
password: hashPassword,
|
|
});
|
|
res.status(201).json({ msg: "Account Created", user: { name, email } });
|
|
} catch (error) {
|
|
res.status(400).json({ msg: "Failed to create Account" });
|
|
}
|
|
};
|
|
|
|
// Login user
|
|
export const Login = async (req, res) => {
|
|
try {
|
|
const user = await Users.findOne({
|
|
where: {
|
|
email: req.body.email,
|
|
},
|
|
});
|
|
|
|
if (!user) {
|
|
return res.status(404).json({ msg: "Email tidak ditemukan" });
|
|
}
|
|
|
|
// Perbaiki akses password (user.password bukan user[0].password)
|
|
const match = await bcrypt.compare(req.body.password, user.password);
|
|
if (!match) return res.status(400).json({ msg: "Wrong Password" });
|
|
|
|
const userId = user.id;
|
|
const name = user.name;
|
|
const email = user.email;
|
|
|
|
const accessToken = jwt.sign(
|
|
{ userId, name, email },
|
|
process.env.ACCESS_TOKEN_SECRET,
|
|
{ expiresIn: "1d" }
|
|
);
|
|
|
|
const refreshToken = jwt.sign(
|
|
{ userId, name, email },
|
|
process.env.REFRESH_TOKEN_SECRET,
|
|
{ expiresIn: "1d" }
|
|
);
|
|
|
|
await Users.update(
|
|
{ refresh_token: refreshToken },
|
|
{ where: { id: userId } }
|
|
);
|
|
|
|
res.cookie("refreshToken", refreshToken, {
|
|
httpOnly: true,
|
|
maxAge: 24 * 60 * 60 * 1000,
|
|
});
|
|
|
|
res.json({ accessToken });
|
|
} catch (error) {
|
|
console.error("Error saat login:", error);
|
|
res.status(500).json({ msg: "Internal Server Error" });
|
|
}
|
|
};
|
|
|
|
export const Logout = async (req, res) => {
|
|
const refreshToken = req.cookies.refreshToken;
|
|
if (!refreshToken) return res.sendStatus(204);
|
|
const user = await Users.findAll({
|
|
where: {
|
|
refresh_token: refreshToken,
|
|
},
|
|
});
|
|
if (!user[0]) return res.json.sendStatus(204);
|
|
const userId = user[0].id;
|
|
await Users.update(
|
|
{ refresh_token: null },
|
|
{
|
|
where: {
|
|
id: userId,
|
|
},
|
|
}
|
|
);
|
|
res.clearCookie("refreshToken");
|
|
return res.sendStatus(200);
|
|
};
|