refactor: reorganize auth routes by separating view and API endpoints. Added some layer security to the most private apis
This commit is contained in:
@@ -5,6 +5,12 @@ const { query } = require('../storage/database');
|
||||
const userAuth = require('../middlewares/user.security');
|
||||
const internalAuth = require('../middlewares/internal.security');
|
||||
|
||||
// ─── VALIDAZIONE INPUT ──────────────────────────────────────────────
|
||||
const USERNAME_REGEX = /^[a-zA-Z0-9_.\-]{3,50}$/;
|
||||
const TELEGRAM_ID_REGEX = /^[0-9]{5,15}$/;
|
||||
|
||||
// ─── ROTTE INTERNAL (prima del router.use userAuth) ─────────────────
|
||||
|
||||
router.get('/', internalAuth, async (req, res) => {
|
||||
try {
|
||||
const result = await query(
|
||||
@@ -12,9 +18,10 @@ router.get('/', internalAuth, async (req, res) => {
|
||||
);
|
||||
res.json(result.rows);
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: 'Errore interno del server ' + err });
|
||||
console.error('[USERS] Errore lista utenti:', err);
|
||||
res.status(500).json({ error: 'Errore interno del server' });
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
router.get('/tonotify', internalAuth, async (req, res) => {
|
||||
try {
|
||||
@@ -23,9 +30,12 @@ router.get('/tonotify', internalAuth, async (req, res) => {
|
||||
);
|
||||
res.json(result.rows);
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: 'Errore interno del server ' + err });
|
||||
console.error('[USERS] Errore lista notifiche:', err);
|
||||
res.status(500).json({ error: 'Errore interno del server' });
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
// ─── ROTTE USER (tutte le rotte sotto usano userAuth) ───────────────
|
||||
|
||||
router.use(userAuth);
|
||||
|
||||
@@ -47,23 +57,33 @@ router.get('/me', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// 2. Modificare l'username dell'utente
|
||||
router.put('/me/username', async (req, res) => {
|
||||
// Si aspetta il nuovo parametro via query (?newUsername=Mario) o body se preferibile
|
||||
const newUsername = req.query.newUsername || req.body?.newUsername;
|
||||
|
||||
if (!newUsername) {
|
||||
if (!newUsername || typeof newUsername !== 'string') {
|
||||
return res.status(400).json({ error: 'Nuovo username richiesto' });
|
||||
}
|
||||
|
||||
// Validazione formato username
|
||||
if (!USERNAME_REGEX.test(newUsername)) {
|
||||
return res.status(400).json({
|
||||
error: 'Username non valido. Deve contenere 3-50 caratteri alfanumerici, underscore, punto o trattino.'
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
await query(
|
||||
'UPDATE users SET username = $1 WHERE id = $2',
|
||||
[newUsername, req.user.user_id] // Nessuna informazione identitaria prelevata dal body
|
||||
const result = await query(
|
||||
'UPDATE users SET username = $1 WHERE id = $2 RETURNING username',
|
||||
[newUsername, req.user.user_id]
|
||||
);
|
||||
res.json({ success: true, message: 'Username aggiornato con successo' });
|
||||
|
||||
if (result.rowCount === 0) {
|
||||
return res.status(404).json({ error: 'Utente non trovato' });
|
||||
}
|
||||
|
||||
res.json({ success: true, username: result.rows[0].username });
|
||||
} catch (err) {
|
||||
if (err.code === '23505') { // UNIQUE constraint violation PostgreSQL
|
||||
if (err.code === '23505') {
|
||||
return res.status(409).json({ error: 'Questo username è già in uso' });
|
||||
}
|
||||
console.error('[USERS] Errore aggiornamento username:', err);
|
||||
@@ -71,22 +91,31 @@ router.put('/me/username', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// 3. Modificare altri parametri (es. telegram_id)
|
||||
router.put('/me/telegram', async (req, res) => {
|
||||
const telegramId = req.query.telegramId || req.body?.telegramId;
|
||||
|
||||
if (!telegramId) {
|
||||
if (!telegramId || typeof telegramId !== 'string') {
|
||||
return res.status(400).json({ error: 'Telegram ID richiesto' });
|
||||
}
|
||||
|
||||
// Validazione formato Telegram ID (solo numeri, 5-15 cifre)
|
||||
if (!TELEGRAM_ID_REGEX.test(telegramId)) {
|
||||
return res.status(400).json({
|
||||
error: 'Telegram ID non valido. Deve contenere solo numeri (5-15 cifre).'
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
await query(
|
||||
'UPDATE users SET telegram_id = $1 WHERE id = $2',
|
||||
[telegramId, req.user.user_id]
|
||||
);
|
||||
res.json({ success: true, message: 'Telegram ID aggiornato con successo' });
|
||||
res.json({ success: true, message: 'Telegram ID aggiornato' });
|
||||
} catch (err) {
|
||||
console.error('[USERS] Errore aggiornamento parametro:', err);
|
||||
if (err.code === '23505') {
|
||||
return res.status(409).json({ error: 'Questo Telegram ID è già associato a un altro account' });
|
||||
}
|
||||
console.error('[USERS] Errore aggiornamento telegram:', err);
|
||||
res.status(500).json({ error: 'Errore interno del server' });
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user