diff --git a/libs/common/src/auth/services/auth.service.ts b/libs/common/src/auth/services/auth.service.ts index f1c1c0d..8f1080c 100644 --- a/libs/common/src/auth/services/auth.service.ts +++ b/libs/common/src/auth/services/auth.service.ts @@ -17,10 +17,21 @@ export class AuthService { private readonly configService: ConfigService, ) {} - async validateUser(email: string, pass: string): Promise { + async validateUser( + email: string, + pass: string, + regionUuid?: string, + ): Promise { const user = await this.userRepository.findOne({ where: { email, + region: regionUuid + ? { + uuid: regionUuid, + } + : { + regionName: 'United Arab Emirates', + }, }, relations: ['roles.roleType'], }); diff --git a/src/auth/dtos/user-auth.dto.ts b/src/auth/dtos/user-auth.dto.ts index 1d6e2a5..b934c4c 100644 --- a/src/auth/dtos/user-auth.dto.ts +++ b/src/auth/dtos/user-auth.dto.ts @@ -1,5 +1,5 @@ import { ApiProperty } from '@nestjs/swagger'; -import { IsEmail, IsNotEmpty, IsString } from 'class-validator'; +import { IsEmail, IsNotEmpty, IsOptional, IsString } from 'class-validator'; import { IsPasswordStrong } from 'src/validators/password.validator'; export class UserSignUpDto { @@ -38,4 +38,8 @@ export class UserSignUpDto { @IsString() @IsNotEmpty() public lastName: string; + + @IsString() + @IsOptional() + public regionUuid: string; } diff --git a/src/auth/dtos/user-login.dto.ts b/src/auth/dtos/user-login.dto.ts index 6a14047..1f3662b 100644 --- a/src/auth/dtos/user-login.dto.ts +++ b/src/auth/dtos/user-login.dto.ts @@ -11,4 +11,9 @@ export class UserLoginDto { @IsString() @IsOptional() password: string; + + @ApiProperty() + @IsString() + @IsOptional() + regionUuid?: string; } diff --git a/src/auth/services/user-auth.service.ts b/src/auth/services/user-auth.service.ts index a514c1c..f464837 100644 --- a/src/auth/services/user-auth.service.ts +++ b/src/auth/services/user-auth.service.ts @@ -44,9 +44,17 @@ export class UserAuthService { ); try { + const { regionUuid, ...rest } = userSignUpDto; const user = await this.userRepository.save({ - ...userSignUpDto, + ...rest, password: hashedPassword, + region: regionUuid + ? { + uuid: regionUuid, + } + : { + regionName: 'United Arab Emirates', + }, }); return user; @@ -80,7 +88,11 @@ export class UserAuthService { } async userLogin(data: UserLoginDto) { - const user = await this.authService.validateUser(data.email, data.password); + const user = await this.authService.validateUser( + data.email, + data.password, + data.regionUuid, + ); if (!user) { throw new UnauthorizedException('Invalid login credentials.');