From b855ad2cb2897dcf8ad54cd57d9dab081eb46e1b Mon Sep 17 00:00:00 2001 From: Vinayak Anand Date: Mon, 20 May 2024 21:12:33 +0530 Subject: [PATCH 1/2] [backend] track last login, and joining time for register and login routes. --- server/src/auth/auth.service.ts | 5 ++++- server/src/users/users.service.ts | 10 ++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/server/src/auth/auth.service.ts b/server/src/auth/auth.service.ts index 7b5e6e9..8567f03 100644 --- a/server/src/auth/auth.service.ts +++ b/server/src/auth/auth.service.ts @@ -25,9 +25,12 @@ export class AuthService { const match = await bcrypt.compare(password, user?.hashedPassword); if (!match) { - throw new UnauthorizedException(); + throw new UnauthorizedException("Password or email doesn't match"); } + user.lastLogin = new Date(); + await user.save(); + await this.mailer.sendTemplateMail({ templateType: 'login', recipients: [email], diff --git a/server/src/users/users.service.ts b/server/src/users/users.service.ts index be64c2d..bce00dd 100644 --- a/server/src/users/users.service.ts +++ b/server/src/users/users.service.ts @@ -66,7 +66,13 @@ export class UsersService { } async create(createUserDto: CreateUserDto): Promise { - const createdUser = new this.model(createUserDto); + const now = new Date(); + + const createdUser = new this.model({ + ...createUserDto, + lastLogin: now, + joined: now, + }); return await createdUser.save(); } @@ -87,7 +93,7 @@ export class UsersService { return String(user._id); } - async findOneByEmail(email: string): Promise { + async findOneByEmail(email: string): Promise { return await this.model.findOne({ email: email }); } From 5d226d29ba9218aa56453d3d535b21dccd3a3fc1 Mon Sep 17 00:00:00 2001 From: Vinayak-Anand Date: Tue, 21 May 2024 00:18:31 +0530 Subject: [PATCH 2/2] [backend] fix code consistency issues --- server/src/auth/auth.service.ts | 2 +- server/src/users/users.service.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/server/src/auth/auth.service.ts b/server/src/auth/auth.service.ts index 8567f03..266e14a 100644 --- a/server/src/auth/auth.service.ts +++ b/server/src/auth/auth.service.ts @@ -25,7 +25,7 @@ export class AuthService { const match = await bcrypt.compare(password, user?.hashedPassword); if (!match) { - throw new UnauthorizedException("Password or email doesn't match"); + throw new UnauthorizedException('Password or email does not match'); } user.lastLogin = new Date(); diff --git a/server/src/users/users.service.ts b/server/src/users/users.service.ts index bce00dd..a0138cc 100644 --- a/server/src/users/users.service.ts +++ b/server/src/users/users.service.ts @@ -67,7 +67,6 @@ export class UsersService { async create(createUserDto: CreateUserDto): Promise { const now = new Date(); - const createdUser = new this.model({ ...createUserDto, lastLogin: now,