server: auth: correctly destory user's service when delete user

This commit is contained in:
Jesse Chan
2020-10-10 01:42:02 +08:00
parent dbde60b35e
commit 2940c2379b
3 changed files with 19 additions and 9 deletions
+6 -2
View File
@@ -126,7 +126,10 @@ class Users {
return undefined;
}
removeUser(username: Credentials['username'], callback: (data: Credentials | null, error?: Error) => void): void {
removeUser(
username: Credentials['username'],
callback: (userId: UserInDatabase['_id'] | null, error?: Error) => void,
): void {
this.db.findOne({username}, (findError: Error | null, user: UserInDatabase): void => {
if (findError) {
return callback(null, findError);
@@ -137,6 +140,7 @@ class Users {
return callback(null, new Error('User not found.'));
}
const userId = user._id;
this.db.remove({username}, {}, (removeError) => {
if (removeError) {
return callback(null, removeError);
@@ -144,7 +148,7 @@ class Users {
fs.rmdirSync(path.join(config.dbPath, user._id), {recursive: true});
return callback({username});
return callback(userId);
});
return undefined;
+12 -5
View File
@@ -187,10 +187,17 @@ router.get('/users', (req, res) => {
});
router.delete('/users/:username', (req, res) => {
Users.removeUser(req.params.username, ajaxUtil.getResponseFn(res));
if (req.user != null) {
services.destroyUserServices(req.user);
}
const callback = ajaxUtil.getResponseFn(res);
Users.removeUser(req.params.username, (id, err) => {
if (err || id == null) {
callback(null, err || new Error());
return;
}
services.destroyUserServices(id);
callback({usernmae: req.params.username});
});
});
router.patch('/users/:username', (req, res) => {
@@ -212,7 +219,7 @@ router.patch('/users/:username', (req, res) => {
}
if (user != null) {
services.destroyUserServices(user);
services.destroyUserServices(user._id);
services.bootstrapServicesForUser(user);
}
+1 -2
View File
@@ -118,8 +118,7 @@ const createUserServices = (user: UserInDatabase): boolean => {
});
};
const destroyUserServices = (user: UserInDatabase) => {
const userId = user._id;
const destroyUserServices = (userId: UserInDatabase['_id']) => {
Object.keys(serviceInstances).forEach((key) => {
const serviceMap = key as keyof typeof serviceInstances;
const userService = serviceInstances[serviceMap][userId];