Inactive admin user api updated
This commit is contained in:
parent
046ce59952
commit
de921c6554
@ -26,6 +26,81 @@ exports.register = async (req, res) => {
|
||||
}
|
||||
};
|
||||
|
||||
//Admin user and Establishment user login
|
||||
exports.login = async (req, res) => {
|
||||
try {
|
||||
const { email, password } = req.body;
|
||||
|
||||
let userRole = null;
|
||||
let userData = null;
|
||||
|
||||
// Try EstablishmentUser first
|
||||
userData = await EstablishmentUser.findOne({ where: { email } });
|
||||
if (userData) {
|
||||
userRole = "EstablishmentUser";
|
||||
} else {
|
||||
// Try Admin user
|
||||
userData = await User.findOne({ where: { email } });
|
||||
if (userData) {
|
||||
userRole = "Admin";
|
||||
}
|
||||
}
|
||||
|
||||
// No user found
|
||||
if (!userData) {
|
||||
return res.status(404).json({ status: "failed", message: "Invalid user", data: "" });
|
||||
}
|
||||
|
||||
// Check ACTIVE status
|
||||
if (!userData.is_active) {
|
||||
return res.status(403).json({ status: "failed", message: "User account is inactive", data: "" });
|
||||
}
|
||||
|
||||
// Check password
|
||||
const validPass = await bcrypt.compare(password, userData.password);
|
||||
if (!validPass) {
|
||||
return res.status(401).json({ status: "failed", message: "Invalid password", data: "" });
|
||||
}
|
||||
|
||||
// Prepare token data
|
||||
let tokenData = {
|
||||
id: userData.id,
|
||||
email: userData.email,
|
||||
name: userData.name,
|
||||
role: userRole,
|
||||
last_login: userData.last_login,
|
||||
};
|
||||
|
||||
if (userRole === "EstablishmentUser") {
|
||||
tokenData.establishment_id = userData.establishment_id;
|
||||
tokenData.establishment_data = await Establishment.findByPk(userData.establishment_id);
|
||||
|
||||
await EstablishmentUser.update(
|
||||
{ last_login: new Date(), updated_at: new Date() },
|
||||
{ where: { id: userData.id } }
|
||||
);
|
||||
} else {
|
||||
await User.update(
|
||||
{ last_login: new Date() },
|
||||
{ where: { id: userData.id } }
|
||||
);
|
||||
}
|
||||
|
||||
// Generate JWT token
|
||||
const token = jwt.sign(tokenData, process.env.JWT_SECRET, {
|
||||
expiresIn: "6h",
|
||||
});
|
||||
|
||||
return res.status(200).json({ status: "success", message: "Login successful", data: token });
|
||||
|
||||
} catch (err) {
|
||||
return res.status(500).json({
|
||||
status: "failed",
|
||||
message: err.message,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// Login user
|
||||
// exports.login = async (req, res) => {
|
||||
// try {
|
||||
@ -65,92 +140,3 @@ exports.register = async (req, res) => {
|
||||
// }
|
||||
// };
|
||||
|
||||
exports.login = async (req, res) => {
|
||||
try {
|
||||
const { email, password } = req.body;
|
||||
|
||||
let userRole = null;
|
||||
let userData = null;
|
||||
|
||||
// Try EstablishmentUser first
|
||||
userData = await EstablishmentUser.findOne({ where: { email } });
|
||||
if (userData) {
|
||||
userRole = "EstablishmentUser";
|
||||
} else {
|
||||
// Try Admin user
|
||||
userData = await User.findOne({ where: { email } });
|
||||
if (userData) {
|
||||
userRole = "Admin";
|
||||
}
|
||||
}
|
||||
|
||||
// No user found
|
||||
if (!userData) {
|
||||
return res.status(404).json({
|
||||
status: "failed",
|
||||
message: "Invalid user",
|
||||
data: ""
|
||||
});
|
||||
}
|
||||
|
||||
// Check ACTIVE status
|
||||
if (!userData.is_active) {
|
||||
return res.status(403).json({
|
||||
status: "failed",
|
||||
message: "User account is inactive",
|
||||
data: ""
|
||||
});
|
||||
}
|
||||
|
||||
// Check password
|
||||
const validPass = await bcrypt.compare(password, userData.password);
|
||||
if (!validPass) {
|
||||
return res.status(401).json({
|
||||
status: "failed",
|
||||
message: "Invalid password",
|
||||
data: ""
|
||||
});
|
||||
}
|
||||
|
||||
// Prepare token data
|
||||
let tokenData = {
|
||||
id: userData.id,
|
||||
email: userData.email,
|
||||
name: userData.name,
|
||||
role: userRole,
|
||||
last_login: userData.last_login,
|
||||
};
|
||||
|
||||
if (userRole === "EstablishmentUser") {
|
||||
tokenData.establishment_id = userData.establishment_id;
|
||||
tokenData.establishment_data = await Establishment.findByPk(userData.establishment_id);
|
||||
|
||||
await EstablishmentUser.update(
|
||||
{ last_login: new Date(), updated_at: new Date() },
|
||||
{ where: { id: userData.id } }
|
||||
);
|
||||
} else {
|
||||
await User.update(
|
||||
{ last_login: new Date() },
|
||||
{ where: { id: userData.id } }
|
||||
);
|
||||
}
|
||||
|
||||
// Generate JWT token
|
||||
const token = jwt.sign(tokenData, process.env.JWT_SECRET, {
|
||||
expiresIn: "6h",
|
||||
});
|
||||
|
||||
return res.status(200).json({
|
||||
status: "success",
|
||||
message: "Login successful",
|
||||
data: token
|
||||
});
|
||||
|
||||
} catch (err) {
|
||||
return res.status(500).json({
|
||||
status: "failed",
|
||||
message: err.message,
|
||||
});
|
||||
}
|
||||
};
|
||||
@ -59,8 +59,8 @@ exports.getUserById = async (req, res) => {
|
||||
exports.updateUser = async (req, res) => {
|
||||
try {
|
||||
|
||||
const { name, email } = req.body;
|
||||
const [updated] = await User.update({ name, email }, { where: { id: req.params.id } });
|
||||
const { name, email,is_active } = req.body;
|
||||
const [updated] = await User.update({ name, email, is_active}, { where: { id: req.params.id } });
|
||||
|
||||
if (updated) {
|
||||
const updatedUser = await User.findByPk(req.params.id);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user