Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

omitting sensitive data before returning

Blue Mousefa89b97585a6
diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts
index 4ab18fa0c..c8a3a6747 100644
--- a/src/auth/auth.controller.ts
+++ b/src/auth/auth.controller.ts
@@ -55,7 +55,7 @@ export class AuthController {
)
}
- return user
+ return this.userService.omitSensitiveData(user)
}
@Post('/login')
diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts
index c7f49ad53..0a985cd6b 100644
--- a/src/user/user.controller.ts
+++ b/src/user/user.controller.ts
@@ -203,7 +203,8 @@ export class UserController {
await this.userService.create({ ...userDTO })
}
await this.userService.update(id, { ...user, ...userDTO })
- return this.userService.findOneWhere({ id })
+ // omit sensitive data before returning using this.userService.omitSensitiveData function
+ return this.userService.omitSensitiveData(await this.userService.findOne(id))
} catch (e) {
if (e.code === 'ER_DUP_ENTRY'){
if (e.sqlMessage.includes(userDTO.email)) {
@@ -242,9 +243,10 @@ export class UserController {
}
// delete internal properties from userDTO before updating it
const userToUpdate = _omit(userDTO, ['id', 'isActive', 'evWarningSentOn', 'exportedAt', 'subID', 'subUpdateURL', 'subCancelURL', 'projects', 'actionTokens', 'roles', 'created', 'updated', 'planCode'])
- console.log(userToUpdate)
await this.userService.update(id, userToUpdate)
- return this.userService.findOneWhere({ id })
+
+ const updatedUser = await this.userService.findOneWhere({ id })
+ return this.userService.omitSensitiveData(updatedUser)
} catch (e) {
throw new BadRequestException(e.message)
}
diff --git a/src/user/user.service.ts b/src/user/user.service.ts
index fbed23ba6..5cc3d501d 100644
--- a/src/user/user.service.ts
+++ b/src/user/user.service.ts
@@ -3,6 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import * as _isEmpty from 'lodash/isEmpty'
import * as _size from 'lodash/size'
+import * as _omit from 'lodash/omit'
import { Pagination, PaginationOptionsInterface } from '../common/pagination'
import { User } from './entities/user.entity'
@@ -51,6 +52,10 @@ export class UserService {
return await this.usersRepository.count()
}
+ omitSensitiveData(user: User): User {
+ return _omit(user, ['password'])
+ }
+
findOneWhere(where: Record<string, unknown>, relations: string[] = []): Promise<User> {
return this.usersRepository.findOne({ where, relations })
}