Actualizar server.js
This commit is contained in:
22
server.js
22
server.js
@@ -477,13 +477,11 @@ app.post("/auth/login", async (req, res) => {
|
|||||||
const { email, password } = req.body;
|
const { email, password } = req.body;
|
||||||
if (!email || !password) return res.status(400).json({ ok: false, error: "Faltan datos" });
|
if (!email || !password) return res.status(400).json({ ok: false, error: "Faltan datos" });
|
||||||
|
|
||||||
// Buscamos al usuario por email o por teléfono
|
|
||||||
const q = await pool.query("SELECT * FROM users WHERE email = $1 OR phone = $1 LIMIT 1", [email]);
|
const q = await pool.query("SELECT * FROM users WHERE email = $1 OR phone = $1 LIMIT 1", [email]);
|
||||||
if (q.rowCount === 0) return res.status(401).json({ ok: false, error: "Credenciales incorrectas" });
|
if (q.rowCount === 0) return res.status(401).json({ ok: false, error: "Credenciales incorrectas" });
|
||||||
|
|
||||||
const user = q.rows[0];
|
const user = q.rows[0];
|
||||||
|
|
||||||
// 👇 NUEVO ESCUDO: Bloquear a los usuarios que no han verificado el WhatsApp
|
|
||||||
if (user.is_verified === false) {
|
if (user.is_verified === false) {
|
||||||
return res.status(403).json({
|
return res.status(403).json({
|
||||||
ok: false,
|
ok: false,
|
||||||
@@ -492,22 +490,24 @@ app.post("/auth/login", async (req, res) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verificar si la cuenta está activa
|
|
||||||
if (user.status !== 'active') return res.status(403).json({ ok: false, error: "Cuenta inactiva o bloqueada" });
|
if (user.status !== 'active') return res.status(403).json({ ok: false, error: "Cuenta inactiva o bloqueada" });
|
||||||
|
|
||||||
// Comprobamos la contraseña
|
|
||||||
const valid = await bcrypt.compare(password, user.password_hash);
|
const valid = await bcrypt.compare(password, user.password_hash);
|
||||||
if (!valid) return res.status(401).json({ ok: false, error: "Credenciales incorrectas" });
|
if (!valid) return res.status(401).json({ ok: false, error: "Credenciales incorrectas" });
|
||||||
|
|
||||||
// Generamos el Token de sesión
|
// 🔥 EL BYPASS DEFINITIVO 🔥
|
||||||
|
// Forzamos el rol a nivel de servidor para que la App no tenga excusas
|
||||||
|
user.role = "operario";
|
||||||
|
|
||||||
const token = signToken(user);
|
const token = signToken(user);
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
ok: true,
|
ok: true,
|
||||||
token,
|
token,
|
||||||
user: {
|
user: {
|
||||||
id: user.id,
|
id: user.id,
|
||||||
full_name: user.full_name,
|
full_name: user.full_name,
|
||||||
role: user.role || 'operario', // 🛡️ ESCUDO: Si la base de datos manda null, forzamos 'operario'
|
role: "operario", // Se lo mandamos a fuego
|
||||||
accountId: user.owner_id || user.id
|
accountId: user.owner_id || user.id
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -517,16 +517,6 @@ app.post("/auth/login", async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/auth/me", authMiddleware, async (req, res) => {
|
|
||||||
try {
|
|
||||||
const q = await pool.query("SELECT id, full_name, email, phone, role, company_slug, plan_tier FROM users WHERE id = $1", [req.user.sub]);
|
|
||||||
if (q.rowCount === 0) return res.status(404).json({ ok: false });
|
|
||||||
res.json({ ok: true, user: q.rows[0] });
|
|
||||||
} catch (e) {
|
|
||||||
res.status(500).json({ ok: false });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// ==========================================
|
// ==========================================
|
||||||
// 📝 REGISTRO DE NUEVAS EMPRESAS (SAAS)
|
// 📝 REGISTRO DE NUEVAS EMPRESAS (SAAS)
|
||||||
// ==========================================
|
// ==========================================
|
||||||
|
|||||||
Reference in New Issue
Block a user