Initial commit: Club DynaSQY

This commit is contained in:
2026-08-23 09:04:47 +00:00
commit 0875890dc7
145 changed files with 16947 additions and 0 deletions
+73
View File
@@ -0,0 +1,73 @@
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const pool = require('../config/db');
const { logActivity } = require('../services/activityLog');
exports.login = async (req, res) => {
try {
const { identifier, password } = req.body;
if (!identifier || !password) {
return res.status(400).json({ error: 'Identifiant et mot de passe requis' });
}
const [rows] = await pool.query(
'SELECT * FROM users WHERE (prenom = ? OR email = ?) AND actif = 1',
[identifier, identifier]
);
if (rows.length === 0) {
return res.status(401).json({ error: 'Identifiants invalides' });
}
const user = rows[0];
const match = await bcrypt.compare(password, user.password_hash);
if (!match) {
return res.status(401).json({ error: 'Identifiants invalides' });
}
const token = jwt.sign(
{ id: user.id, prenom: user.prenom, nom: user.nom, email: user.email, role: user.role },
process.env.JWT_SECRET,
{ expiresIn: '7d' }
);
logActivity(user, 'connexion');
res.json({
token,
user: { id: user.id, prenom: user.prenom, nom: user.nom, societe: user.societe, email: user.email, role: user.role },
});
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Erreur serveur' });
}
};
exports.me = async (req, res) => {
try {
const [rows] = await pool.query(
'SELECT id, prenom, nom, societe, activite, email, telephone, role, date_adhesion, date_echeance FROM users WHERE id = ?',
[req.user.id]
);
if (rows.length === 0) return res.status(404).json({ error: 'Utilisateur introuvable' });
res.json(rows[0]);
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Erreur serveur' });
}
};
exports.changePassword = async (req, res) => {
try {
const { currentPassword, newPassword } = req.body;
if (!currentPassword || !newPassword) {
return res.status(400).json({ error: 'Mot de passe actuel et nouveau requis' });
}
if (newPassword.length < 6) {
return res.status(400).json({ error: 'Le nouveau mot de passe doit faire au moins 6 caractères' });
}
const [rows] = await pool.query('SELECT password_hash FROM users WHERE id = ?', [req.user.id]);
const match = await bcrypt.compare(currentPassword, rows[0].password_hash);
if (!match) return res.status(401).json({ error: 'Mot de passe actuel incorrect' });
const hash = await bcrypt.hash(newPassword, 10);
await pool.query('UPDATE users SET password_hash = ? WHERE id = ?', [hash, req.user.id]);
logActivity(req, 'changement_mot_de_passe');
res.json({ message: 'Mot de passe modifié' });
} catch (err) {
console.error(err);
res.status(500).json({ error: 'Erreur serveur' });
}
};