Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track last login, and joining time for register and login routes #110

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion server/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 does not match');
}

user.lastLogin = new Date();
await user.save();

await this.mailer.sendTemplateMail({
templateType: 'login',
recipients: [email],
Expand Down
9 changes: 7 additions & 2 deletions server/src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ export class UsersService {
}

async create(createUserDto: CreateUserDto): Promise<User> {
const createdUser = new this.model(createUserDto);
const now = new Date();
const createdUser = new this.model({
...createUserDto,
lastLogin: now,
joined: now,
});
return await createdUser.save();
}

Expand All @@ -87,7 +92,7 @@ export class UsersService {
return String(user._id);
}

async findOneByEmail(email: string): Promise<User | undefined> {
async findOneByEmail(email: string): Promise<UserDocument | undefined> {
return await this.model.findOne({ email: email });
}

Expand Down