diff --git a/backend/src/auth/auth.controller.ts b/backend/src/auth/auth.controller.ts index 89b0d3757..4c8b49e3a 100644 --- a/backend/src/auth/auth.controller.ts +++ b/backend/src/auth/auth.controller.ts @@ -45,12 +45,13 @@ export class AuthController { }) async signUp( @Body() dto: AuthRegisterDTO, + @Req() { ip }: Request, @Res({ passthrough: true }) response: Response, ) { if (!this.config.get("share.allowRegistration")) throw new ForbiddenException("Registration is not allowed"); - const result = await this.authService.signUp(dto); + const result = await this.authService.signUp(dto, ip); this.authService.addTokensToResponse( response, @@ -71,9 +72,10 @@ export class AuthController { @HttpCode(200) async signIn( @Body() dto: AuthSignInDTO, + @Req() { ip }: Request, @Res({ passthrough: true }) response: Response, ) { - const result = await this.authService.signIn(dto); + const result = await this.authService.signIn(dto, ip); if (result.accessToken && result.refreshToken) { this.authService.addTokensToResponse( diff --git a/backend/src/auth/auth.service.ts b/backend/src/auth/auth.service.ts index f40b518b1..6d138c241 100644 --- a/backend/src/auth/auth.service.ts +++ b/backend/src/auth/auth.service.ts @@ -2,6 +2,7 @@ import { BadRequestException, ForbiddenException, Injectable, + Logger, UnauthorizedException, } from "@nestjs/common"; import { JwtService } from "@nestjs/jwt"; @@ -24,8 +25,9 @@ export class AuthService { private config: ConfigService, private emailService: EmailService, ) {} + private readonly logger = new Logger(AuthService.name); - async signUp(dto: AuthRegisterDTO) { + async signUp(dto: AuthRegisterDTO, ip: string) { const isFirstUser = (await this.prisma.user.count()) == 0; const hash = dto.password ? await argon.hash(dto.password) : null; @@ -44,6 +46,7 @@ export class AuthService { ); const accessToken = await this.createAccessToken(user, refreshTokenId); + this.logger.log(`User ${user.email} signed up from IP ${ip}`); return { accessToken, refreshToken, user }; } catch (e) { if (e instanceof PrismaClientKnownRequestError) { @@ -57,7 +60,7 @@ export class AuthService { } } - async signIn(dto: AuthSignInDTO) { + async signIn(dto: AuthSignInDTO, ip: string) { if (!dto.email && !dto.username) throw new BadRequestException("Email or username is required"); @@ -67,9 +70,14 @@ export class AuthService { }, }); - if (!user || !(await argon.verify(user.password, dto.password))) + if (!user || !(await argon.verify(user.password, dto.password))) { + this.logger.log( + `Failed login attempt for user ${dto.email} from IP ${ip}`, + ); throw new UnauthorizedException("Wrong email or password"); + } + this.logger.log(`Successful login for user ${dto.email} from IP ${ip}`); return this.generateToken(user); } diff --git a/backend/src/oauth/oauth.controller.ts b/backend/src/oauth/oauth.controller.ts index 63220d94e..bf3824e32 100644 --- a/backend/src/oauth/oauth.controller.ts +++ b/backend/src/oauth/oauth.controller.ts @@ -85,7 +85,7 @@ export class OAuthController { accessToken?: string; refreshToken?: string; loginToken?: string; - } = await this.oauthService.signIn(user); + } = await this.oauthService.signIn(user, request.ip); if (token.accessToken) { this.authService.addTokensToResponse( response, diff --git a/backend/src/oauth/oauth.service.ts b/backend/src/oauth/oauth.service.ts index c44e53ec7..0da039b4b 100644 --- a/backend/src/oauth/oauth.service.ts +++ b/backend/src/oauth/oauth.service.ts @@ -1,4 +1,4 @@ -import { Inject, Injectable } from "@nestjs/common"; +import { Inject, Injectable, Logger } from "@nestjs/common"; import { User } from "@prisma/client"; import { nanoid } from "nanoid"; import { AuthService } from "../auth/auth.service"; @@ -15,6 +15,7 @@ export class OAuthService { private auth: AuthService, @Inject("OAUTH_PLATFORMS") private platforms: string[], ) {} + private readonly logger = new Logger(OAuthService.name); available(): string[] { return this.platforms @@ -39,7 +40,7 @@ export class OAuthService { return Object.fromEntries(oauthUsers.map((u) => [u.provider, u])); } - async signIn(user: OAuthSignInDto) { + async signIn(user: OAuthSignInDto, ip: string) { const oauthUser = await this.prisma.oAuthUser.findFirst({ where: { provider: user.provider, @@ -50,10 +51,11 @@ export class OAuthService { }, }); if (oauthUser) { + this.logger.log(`Successful login for user ${user.email} from IP ${ip}`); return this.auth.generateToken(oauthUser.user, true); } - return this.signUp(user); + return this.signUp(user, ip); } async link( @@ -119,7 +121,7 @@ export class OAuthService { } } - private async signUp(user: OAuthSignInDto) { + private async signUp(user: OAuthSignInDto, ip: string) { // register if (!this.config.get("oauth.allowRegistration")) { throw new ErrorPageException("no_user", "/auth/signIn", [ @@ -151,11 +153,14 @@ export class OAuthService { return this.auth.generateToken(existingUser, true); } - const result = await this.auth.signUp({ - email: user.email, - username: await this.getAvailableUsername(user.providerUsername), - password: null, - }); + const result = await this.auth.signUp( + { + email: user.email, + username: await this.getAvailableUsername(user.providerUsername), + password: null, + }, + ip, + ); await this.prisma.oAuthUser.create({ data: {