mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-29 18:16:17 +01:00
feat(api): account deletion routes for the Write API (#8881)
* feat(api): account deletion routes for the Write API * refactor: rewrite client-side calls to account deletion to use api * style: apply DRY
This commit is contained in:
@@ -15,7 +15,6 @@ const db = require('../database');
|
||||
const userController = require('../controllers/user');
|
||||
const privileges = require('../privileges');
|
||||
const utils = require('../utils');
|
||||
const flags = require('../flags');
|
||||
const sockets = require('.');
|
||||
|
||||
const SocketUser = module.exports;
|
||||
@@ -37,37 +36,8 @@ SocketUser.exists = async function (socket, data) {
|
||||
};
|
||||
|
||||
SocketUser.deleteAccount = async function (socket, data) {
|
||||
if (!socket.uid) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
const hasPassword = await user.hasPassword(socket.uid);
|
||||
if (hasPassword) {
|
||||
const ok = await user.isPasswordCorrect(socket.uid, data.password, socket.ip);
|
||||
if (!ok) {
|
||||
throw new Error('[[error:invalid-password]]');
|
||||
}
|
||||
}
|
||||
const isAdmin = await user.isAdministrator(socket.uid);
|
||||
if (isAdmin) {
|
||||
throw new Error('[[error:cant-delete-admin]]');
|
||||
}
|
||||
if (meta.config.allowAccountDelete !== 1) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
|
||||
await flags.resolveFlag('user', socket.uid, socket.uid);
|
||||
const userData = await user.deleteAccount(socket.uid);
|
||||
|
||||
require('./index').server.sockets.emit('event:user_status_change', { uid: socket.uid, status: 'offline' });
|
||||
|
||||
await events.log({
|
||||
type: 'user-delete',
|
||||
uid: socket.uid,
|
||||
targetUid: socket.uid,
|
||||
ip: socket.ip,
|
||||
username: userData.username,
|
||||
email: userData.email,
|
||||
});
|
||||
sockets.warnDeprecated(socket, 'DELETE /api/v3/users/:uid/account');
|
||||
await api.users.deleteAccount(socket, data);
|
||||
};
|
||||
|
||||
SocketUser.emailExists = async function (socket, data) {
|
||||
|
||||
Reference in New Issue
Block a user