Skip to content

Commit

Permalink
use origin for mailing related operations
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Oct 13, 2024
1 parent 2fca502 commit 3aaf570
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion api/src/modules/auth/authentication.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class AuthenticationController {
authContract.requestPasswordRecovery,
async ({ body: { email } }) => {
await this.commandBus.execute(
new RequestPasswordRecoveryCommand(email),
new RequestPasswordRecoveryCommand(email, origin),
);
return {
body: null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export class RequestPasswordRecoveryCommand {
constructor(public readonly email: string) {}
constructor(
public readonly email: string,
public readonly origin: string,
) {}
}
22 changes: 14 additions & 8 deletions api/src/modules/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { ConflictException, Injectable } from '@nestjs/common';
import {
ConflictException,
Injectable,
UnauthorizedException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from '@shared/entities/users/user.entity';
import { Repository } from 'typeorm';
import * as bcrypt from 'bcrypt';

import { AppBaseService } from '@api/utils/app-base.service';
import { CreateUserDto } from '@shared/dtos/users/create-user.dto';
import { UpdateUserDto } from '@shared/dtos/users/update-user.dto';
import { AppInfoDTO } from '@api/utils/info.dto';
import { RequestEmailUpdateDto } from '@shared/dtos/users/request-email-update.dto';
@Injectable()
export class UsersService extends AppBaseService<
User,
Expand All @@ -31,19 +35,14 @@ export class UsersService extends AppBaseService<
return this.userRepository.findOne({ where: { email } });
}

async createUser(newUser: Partial<User>) {
async saveUser(newUser: Partial<User>) {
const existingUser = await this.findByEmail(newUser.email);
if (existingUser) {
throw new ConflictException(`Email ${newUser.email} already exists`);
}
return this.userRepository.save(newUser);
}

async saveNewHashedPassword(user: User, newPassword: string) {
user.password = await bcrypt.hash(newPassword, 10);
return this.userRepository.save(user);
}

async delete(user: User) {
return this.userRepository.remove(user);
}
Expand All @@ -52,4 +51,11 @@ export class UsersService extends AppBaseService<
const user = await this.userRepository.findOneBy({ id });
return user.isActive;
}

async requestEmailUpdate(user: User, dto: RequestEmailUpdateDto) {
const { email, newEmail } = dto;
if (user.email !== email) {
throw new UnauthorizedException();
}
}
}

0 comments on commit 3aaf570

Please sign in to comment.