Add deleteStorage method to remove user storage data and update routes
Despliegue Automático / desplegar (push) Successful in 1m29s
Despliegue Automático / desplegar (push) Successful in 1m29s
This commit is contained in:
@@ -108,6 +108,29 @@ const deleteAllData = async (req, res) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const deleteStorage = async (req, res) => {
|
||||||
|
try {
|
||||||
|
const authorizationHeader = req.headers.authorization || '';
|
||||||
|
if (!authorizationHeader.startsWith('Bearer ')) {
|
||||||
|
return res.status(401).json({ error: 'Authorization header missing or invalid' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const token = authorizationHeader.slice(7).trim();
|
||||||
|
const payload = jwt.verify(token, JWT_SECRET);
|
||||||
|
const userId = payload && payload.id;
|
||||||
|
if (!userId) return res.status(401).json({ error: 'Usuario inválido en token' });
|
||||||
|
|
||||||
|
const result = await authService.deleteStorage(userId);
|
||||||
|
|
||||||
|
res.json(result);
|
||||||
|
} catch (error) {
|
||||||
|
if (error.name === 'JsonWebTokenError' || error.name === 'TokenExpiredError') {
|
||||||
|
return res.status(401).json({ error: 'Token inválido o expirado' });
|
||||||
|
}
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const getSessionsInfo = async (req, res) => {
|
const getSessionsInfo = async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const authorizationHeader = req.headers.authorization || '';
|
const authorizationHeader = req.headers.authorization || '';
|
||||||
@@ -156,4 +179,4 @@ const logoutDevice = async (req, res) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Exportamos los handlers de auth.
|
// Exportamos los handlers de auth.
|
||||||
module.exports = { register, login, refresh, logout, deleteAllData, getSessionsInfo, logoutDevice };
|
module.exports = { register, login, refresh, logout, deleteAllData, getSessionsInfo, logoutDevice, deleteStorage };
|
||||||
@@ -17,6 +17,8 @@ router.post('/logout', authController.logout);
|
|||||||
// DELETE /api/auth/delete-all-data
|
// DELETE /api/auth/delete-all-data
|
||||||
router.delete('/delete-all-data', authController.deleteAllData);
|
router.delete('/delete-all-data', authController.deleteAllData);
|
||||||
|
|
||||||
|
router.delete('/delete-storage', authController.deleteStorage);
|
||||||
|
|
||||||
router.get('/getSessionsInfo', authController.getSessionsInfo);
|
router.get('/getSessionsInfo', authController.getSessionsInfo);
|
||||||
|
|
||||||
router.post('/logout-device', authController.logoutDevice); // Nueva ruta para cerrar sesión en un dispositivo específico
|
router.post('/logout-device', authController.logoutDevice); // Nueva ruta para cerrar sesión en un dispositivo específico
|
||||||
|
|||||||
@@ -106,6 +106,21 @@ class AuthService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async deleteStorage(userId) {
|
||||||
|
if (!userId) {
|
||||||
|
throw new Error('userId es obligatorio');
|
||||||
|
}
|
||||||
|
|
||||||
|
return sequelize.transaction(async (transaction) => {
|
||||||
|
await Note.destroy({ where: { userId }, transaction });
|
||||||
|
await Category.destroy({ where: { userId }, transaction });
|
||||||
|
|
||||||
|
return {
|
||||||
|
message: 'Datos de almacenamiento del usuario eliminados correctamente; los tokens permanecen intactos.'
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async getSessionsInfo(userId) {
|
async getSessionsInfo(userId) {
|
||||||
if (!userId) {
|
if (!userId) {
|
||||||
throw new Error('userId es obligatorio');
|
throw new Error('userId es obligatorio');
|
||||||
|
|||||||
Reference in New Issue
Block a user