diff --git a/apps/production/src/auth/auth.controller.ts b/apps/production/src/auth/auth.controller.ts index 440ebd7d..966c2657 100644 --- a/apps/production/src/auth/auth.controller.ts +++ b/apps/production/src/auth/auth.controller.ts @@ -151,7 +151,12 @@ export class AuthController { !user.isTwoFactorAuthenticationEnabled, ) - await this.authService.sendTelegramNotification(user.id, headers, ip) + await this.authService.sendTelegramNotification( + 'Someone has logged into your account!', + user.id, + headers, + ip, + ) if (user.isTwoFactorAuthenticationEnabled) { user = _pick(user, ['isTwoFactorAuthenticationEnabled', 'email']) @@ -210,6 +215,13 @@ export class AuthController { throw new ConflictException(i18n.t('auth.accountNotExists')) } + await this.authService.sendTelegramNotification( + 'Someone has requested a password reset!', + user.id, + headers, + ip, + ) + await this.authService.sendResetPasswordEmail(user.id, user.email) } @@ -247,8 +259,12 @@ export class AuthController { @Body() body: ChangePasswordDto, @CurrentUserId() userId: string, @I18n() i18n: I18nContext, + @Headers() headers: unknown, + @Ip() requestIp: string, ): Promise { const user = await this.userService.findUserById(userId) + const ip = + headers['x-forwarded-for'] || headers['cf-connecting-ip'] || requestIp if (!user) { throw new UnauthorizedException() @@ -263,6 +279,13 @@ export class AuthController { throw new ConflictException(i18n.t('auth.invalidPassword')) } + await this.authService.sendTelegramNotification( + 'Someone has changed their password!', + user.id, + headers, + ip, + ) + await this.authService.changePassword(user.id, body.newPassword) } @@ -308,8 +331,12 @@ export class AuthController { @Body() body: RequestChangeEmailDto, @CurrentUserId() userId: string, @I18n() i18n: I18nContext, + @Headers() headers: unknown, + @Ip() requestIp: string, ): Promise { const user = await this.userService.findUserById(userId) + const ip = + headers['x-forwarded-for'] || headers['cf-connecting-ip'] || requestIp if (!user) { throw new UnauthorizedException() @@ -330,6 +357,13 @@ export class AuthController { throw new ConflictException(i18n.t('auth.emailAlreadyTaken')) } + await this.authService.sendTelegramNotification( + 'Someone has changed their email!', + user.id, + headers, + ip, + ) + await this.authService.changeEmail(user.id, user.email, body.newEmail) } @@ -428,13 +462,26 @@ export class AuthController { @UseGuards(JwtRefreshTokenGuard) @Post('logout-all') @HttpCode(200) - public async logoutAll(@CurrentUserId() userId: string): Promise { + public async logoutAll( + @CurrentUserId() userId: string, + @Headers() headers: unknown, + @Ip() requestIp: string, + ): Promise { const user = await this.userService.findUserById(userId) + const ip = + headers['x-forwarded-for'] || headers['cf-connecting-ip'] || requestIp if (!user) { throw new UnauthorizedException() } + await this.authService.sendTelegramNotification( + 'Someone has logged out of all devices!', + user.id, + headers, + ip, + ) + await this.authService.logoutAll(user.id) } diff --git a/apps/production/src/auth/auth.service.ts b/apps/production/src/auth/auth.service.ts index 946d6bab..6dad6d89 100644 --- a/apps/production/src/auth/auth.service.ts +++ b/apps/production/src/auth/auth.service.ts @@ -217,6 +217,7 @@ export class AuthService { } public async sendTelegramNotification( + messageTitle: string, userId: string, headers: unknown, ip: string, @@ -230,7 +231,7 @@ export class AuthService { const headersInfo = await this.getHeadersInfo(headers) const loginDate = dayjs().utc().format('YYYY-MM-DD HH:mm:ss') const message = - '🚨 *Someone has logged into your account!*\n\n' + + `🚨 *${messageTitle}*\n\n` + `*Browser:* ${headersInfo.browser}\n` + `*Device:* ${headersInfo.device}\n` + `*OS:* ${headersInfo.os}\n` + @@ -481,7 +482,12 @@ export class AuthService { throw new BadRequestException() } - await this.sendTelegramNotification(user.id, headers, ip) + await this.sendTelegramNotification( + 'Someone has logged in to their account with Google', + user.id, + headers, + ip, + ) const jwtTokens = await this.generateJwtTokens( user.id, @@ -892,7 +898,12 @@ export class AuthService { throw new BadRequestException() } - await this.sendTelegramNotification(user.id, headers, ip) + await this.sendTelegramNotification( + 'Someone has logged in to their account with Github', + user.id, + headers, + ip, + ) const jwtTokens = await this.generateJwtTokens( user.id,