From 6dd6c79d87ad3e0d831f4da641d7123ba19a6e83 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Wed, 22 Jan 2025 00:34:47 -0600 Subject: [PATCH] Add app agreement acceptance check and validation --- libs/common/src/auth/services/auth.service.ts | 6 +++++- src/auth/dtos/user-auth.dto.ts | 20 ++++++++++++++++++- src/auth/services/user-auth.service.ts | 12 +++++++++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/libs/common/src/auth/services/auth.service.ts b/libs/common/src/auth/services/auth.service.ts index bc25e0e..528db56 100644 --- a/libs/common/src/auth/services/auth.service.ts +++ b/libs/common/src/auth/services/auth.service.ts @@ -48,7 +48,9 @@ export class AuthService { if (!user.isActive) { throw new BadRequestException('User is not active'); } - + if (!user.hasAcceptedAppAgreement) { + throw new BadRequestException('User has not accepted app agreement'); + } const passwordMatch = await this.helperHashService.bcryptCompare( pass, user.password, @@ -92,6 +94,8 @@ export class AuthService { sessionId: user.sessionId, role: user?.role, googleCode: user.googleCode, + hasAcceptedWebAgreement: user.hasAcceptedWebAgreement, + hasAcceptedAppAgreement: user.hasAcceptedAppAgreement, }; if (payload.googleCode) { const profile = await this.getProfile(payload.googleCode); diff --git a/src/auth/dtos/user-auth.dto.ts b/src/auth/dtos/user-auth.dto.ts index dad1e07..d2f2e8a 100644 --- a/src/auth/dtos/user-auth.dto.ts +++ b/src/auth/dtos/user-auth.dto.ts @@ -1,5 +1,11 @@ import { ApiProperty } from '@nestjs/swagger'; -import { IsEmail, IsNotEmpty, IsOptional, IsString } from 'class-validator'; +import { + IsBoolean, + IsEmail, + IsNotEmpty, + IsOptional, + IsString, +} from 'class-validator'; import { IsPasswordStrong } from 'src/validators/password.validator'; export class UserSignUpDto { @@ -39,7 +45,19 @@ export class UserSignUpDto { @IsNotEmpty() public lastName: string; + @ApiProperty({ + description: 'regionUuid', + required: false, + }) @IsString() @IsOptional() public regionUuid?: string; + + @ApiProperty({ + description: 'hasAcceptedAppAgreement', + required: true, + }) + @IsBoolean() + @IsNotEmpty() + public hasAcceptedAppAgreement: boolean; } diff --git a/src/auth/services/user-auth.service.ts b/src/auth/services/user-auth.service.ts index afdf6d2..c9c9436 100644 --- a/src/auth/services/user-auth.service.ts +++ b/src/auth/services/user-auth.service.ts @@ -46,12 +46,17 @@ export class UserAuthService { ); try { - const { regionUuid, ...rest } = userSignUpDto; + const { regionUuid, hasAcceptedAppAgreement, ...rest } = userSignUpDto; + if (!hasAcceptedAppAgreement) { + throw new BadRequestException('Please accept the terms and conditions'); + } const spaceMemberRole = await this.roleService.findRoleByType( RoleType.SPACE_MEMBER, ); const user = await this.userRepository.save({ ...rest, + appAgreementAcceptedAt: new Date(), + hasAcceptedAppAgreement, password: hashedPassword, roleType: { uuid: spaceMemberRole.uuid }, region: regionUuid @@ -65,7 +70,7 @@ export class UserAuthService { return user; } catch (error) { - throw new BadRequestException('Failed to register user'); + throw new BadRequestException(error.message || 'Failed to register user'); } } @@ -116,6 +121,7 @@ export class UserAuthService { firstName: googleUserData['given_name'], lastName: googleUserData['family_name'], password: googleUserData['email'], + hasAcceptedAppAgreement: true, }); } data.email = googleUserData['email']; @@ -147,6 +153,8 @@ export class UserAuthService { userId: user.uuid, uuid: user.uuid, role: user.roleType, + hasAcceptedWebAgreement: user.hasAcceptedWebAgreement, + hasAcceptedAppAgreement: user.hasAcceptedAppAgreement, sessionId: session[1].uuid, }); return res;