From 490105c21f3c0fcf8fc5061d65a2df1ef7c692af Mon Sep 17 00:00:00 2001 From: Vinayak Anand Date: Mon, 20 May 2024 20:49:51 +0530 Subject: [PATCH] [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 }); }