Skip to content

Commit c0d3858

Browse files
authored
fix(validations): add some required validations and bypass for testing
* feat(verification): add config service for bypassing OTP validation in tests * feat(validations): add AgeRangeValidator for birth date validation * refactor(user): remove unnecessary Exclude decorators from user entity fields and use instanceToPlain for user objects in responses * feat(validations): add max length validation to various DTOs
1 parent 5088569 commit c0d3858

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+322
-106
lines changed

package-lock.json

Lines changed: 29 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/auth/auth.controller.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -509,18 +509,22 @@ export class AuthController {
509509
@Body() dto: MobileGitHubAuthDto,
510510
@Res({ passthrough: true }) response: Response
511511
) {
512-
const result = await this.auth_service.verifyGitHubMobileToken(dto.code, dto.redirect_uri, dto.code_verifier);
512+
const result = await this.auth_service.verifyGitHubMobileToken(
513+
dto.code,
514+
dto.redirect_uri,
515+
dto.code_verifier
516+
);
513517

514518
if ('needs_completion' in result && result.needs_completion) {
515-
const sessionToken = await this.auth_service.createOAuthSession(result.user);
519+
const session_token = await this.auth_service.createOAuthSession(result.user);
516520
return {
517521
needs_completion: true,
518-
session_token: sessionToken,
522+
session_token: session_token,
519523
provider: 'github',
520524
};
521525
}
522526

523-
if (!("user" in result) || !("id" in result.user)) {
527+
if (!('user' in result) || !('id' in result.user)) {
524528
throw new BadRequestException(ERROR_MESSAGES.GITHUB_TOKEN_INVALID);
525529
}
526530

src/auth/auth.service.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ export class AuthService {
259259
await this.redis_service.del(otp_key);
260260

261261
return {
262-
user: created_user,
262+
user: instanceToPlain(created_user),
263263
access_token,
264264
refresh_token,
265265
};
@@ -311,7 +311,7 @@ export class AuthService {
311311
const { access_token, refresh_token } = await this.generateTokens(id);
312312

313313
return {
314-
user: user,
314+
user: instanceToPlain(user),
315315
access_token: access_token,
316316
refresh_token: refresh_token,
317317
};
@@ -629,7 +629,7 @@ export class AuthService {
629629

630630
if (user) {
631631
return {
632-
user: user,
632+
user: instanceToPlain(user),
633633
needs_completion: false,
634634
};
635635
}
@@ -655,7 +655,7 @@ export class AuthService {
655655
}
656656

657657
return {
658-
user: updated_user,
658+
user: instanceToPlain(updated_user),
659659
needs_completion: false,
660660
};
661661
}
@@ -736,7 +736,7 @@ export class AuthService {
736736
let user = await this.user_repository.findByGithubId(github_user.github_id);
737737
if (user) {
738738
return {
739-
user: user,
739+
user: instanceToPlain(user),
740740
needs_completion: false,
741741
};
742742
}
@@ -758,7 +758,7 @@ export class AuthService {
758758
}
759759

760760
return {
761-
user: updated_user,
761+
user: instanceToPlain(updated_user),
762762
needs_completion: false,
763763
};
764764
}

src/auth/dto/change-password-auth.dto.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { IsNotEmpty, Matches, MinLength } from 'class-validator';
1+
import { IsNotEmpty, Matches, MaxLength, MinLength } from 'class-validator';
22
import { ApiProperty } from '@nestjs/swagger';
3+
import { STRING_MAX_LENGTH } from 'src/constants/variables';
34

45
export class ChangePasswordAuthDTO {
56
@ApiProperty({
@@ -10,6 +11,7 @@ export class ChangePasswordAuthDTO {
1011
})
1112
@IsNotEmpty()
1213
@MinLength(8)
14+
@MaxLength(STRING_MAX_LENGTH)
1315
@Matches(/((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/, {
1416
message:
1517
'Password must contain at least one uppercase letter, one lowercase letter, and one number or special character',
@@ -24,6 +26,7 @@ export class ChangePasswordAuthDTO {
2426
})
2527
@IsNotEmpty()
2628
@MinLength(8)
29+
@MaxLength(STRING_MAX_LENGTH)
2730
@Matches(/((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$/, {
2831
message:
2932
'Password must contain at least one uppercase letter, one lowercase letter, and one number or special character',
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ApiProperty } from '@nestjs/swagger';
2-
import { IsNotEmpty, IsString } from 'class-validator';
2+
import { IsNotEmpty, IsString, MaxLength } from 'class-validator';
3+
import { STRING_MAX_LENGTH } from 'src/constants/variables';
34

45
export class CheckIdentifierDto {
56
@ApiProperty({
@@ -8,5 +9,6 @@ export class CheckIdentifierDto {
89
})
910
@IsString()
1011
@IsNotEmpty()
12+
@MaxLength(STRING_MAX_LENGTH)
1113
identifier: string;
1214
}

src/auth/dto/facebook-login.dto.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
import { IsEmail, IsNotEmpty, IsOptional, IsString } from 'class-validator';
1+
import { IsEmail, IsNotEmpty, IsOptional, IsString, MaxLength } from 'class-validator';
2+
import { STRING_MAX_LENGTH } from 'src/constants/variables';
23

34
export class FacebookLoginDTO {
45
@IsString()
6+
@MaxLength(STRING_MAX_LENGTH)
57
facebook_id: string;
68

79
@IsEmail()
10+
@MaxLength(STRING_MAX_LENGTH)
811
email: string;
912

1013
@IsNotEmpty()
1114
@IsString()
15+
@MaxLength(STRING_MAX_LENGTH)
1216
first_name: string;
1317

1418
@IsNotEmpty()
1519
@IsString()
20+
@MaxLength(STRING_MAX_LENGTH)
1621
last_name: string;
1722

1823
@IsOptional()
1924
@IsString()
25+
@MaxLength(500)
2026
avatar_url?: string;
2127
}

src/auth/dto/forget-password.dto.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { IsNotEmpty, IsString } from 'class-validator';
1+
import { IsNotEmpty, IsString, MaxLength } from 'class-validator';
22
import { ApiProperty } from '@nestjs/swagger';
3+
import { STRING_MAX_LENGTH } from 'src/constants/variables';
34

45
export class ForgetPasswordDto {
56
// email - username - phone_number
@@ -9,5 +10,6 @@ export class ForgetPasswordDto {
910
})
1011
@IsString()
1112
@IsNotEmpty()
13+
@MaxLength(STRING_MAX_LENGTH)
1214
identifier: string;
1315
}

src/auth/dto/github-user.dto.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
import { IsEmail, IsNotEmpty, IsOptional, IsString } from 'class-validator';
1+
import { IsEmail, IsNotEmpty, IsOptional, IsString, MaxLength } from 'class-validator';
22
import { ApiProperty } from '@nestjs/swagger';
3+
import { STRING_MAX_LENGTH } from 'src/constants/variables';
34

45
export class GitHubUserDto {
56
@ApiProperty({
67
description: 'GitHub user ID',
78
example: '12345678',
89
})
910
@IsString()
11+
@MaxLength(STRING_MAX_LENGTH)
1012
github_id: string;
1113

1214
@ApiProperty({
1315
description: 'User email from GitHub',
1416
example: 'shady@example.com',
1517
})
1618
@IsEmail()
19+
@MaxLength(STRING_MAX_LENGTH)
1720
email: string;
1821

1922
@ApiProperty({
@@ -22,13 +25,15 @@ export class GitHubUserDto {
2225
})
2326
@IsString()
2427
@IsNotEmpty()
28+
@MaxLength(STRING_MAX_LENGTH)
2529
first_name: string;
2630

2731
@ApiProperty({
2832
description: 'Last name from GitHub profile',
2933
example: 'Raafat',
3034
})
3135
@IsString()
36+
@MaxLength(STRING_MAX_LENGTH)
3237
last_name: string;
3338

3439
@ApiProperty({
@@ -38,5 +43,6 @@ export class GitHubUserDto {
3843
})
3944
@IsOptional()
4045
@IsString()
46+
@MaxLength(500)
4147
avatar_url?: string;
4248
}

src/auth/dto/google-login.dto.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
import { IsEmail, IsNotEmpty, IsOptional, IsString } from 'class-validator';
1+
import { IsEmail, IsNotEmpty, IsOptional, IsString, MaxLength } from 'class-validator';
2+
import { STRING_MAX_LENGTH } from 'src/constants/variables';
23

34
export class GoogleLoginDTO {
45
@IsString()
6+
@MaxLength(STRING_MAX_LENGTH)
57
google_id: string;
68

79
@IsEmail()
10+
@MaxLength(STRING_MAX_LENGTH)
811
email: string;
912

1013
@IsNotEmpty()
1114
@IsString()
15+
@MaxLength(STRING_MAX_LENGTH)
1216
first_name: string;
1317

1418
@IsNotEmpty()
1519
@IsString()
20+
@MaxLength(STRING_MAX_LENGTH)
1621
last_name: string;
1722

1823
@IsOptional()
1924
@IsString()
25+
@MaxLength(500)
2026
avatar_url?: string;
2127
}

src/auth/dto/login.dto.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { IsIn, IsNotEmpty, IsString, Matches, MinLength } from 'class-validator';
1+
import { IsIn, IsNotEmpty, IsString, Matches, MaxLength, MinLength } from 'class-validator';
22
import { ApiProperty } from '@nestjs/swagger';
3+
import { STRING_MAX_LENGTH } from 'src/constants/variables';
34

45
export class LoginDTO {
56
@ApiProperty({
@@ -9,6 +10,7 @@ export class LoginDTO {
910
})
1011
@IsString()
1112
@IsNotEmpty()
13+
@MaxLength(STRING_MAX_LENGTH)
1214
identifier: string;
1315

1416
@ApiProperty({
@@ -17,6 +19,7 @@ export class LoginDTO {
1719
})
1820
@IsString()
1921
@IsNotEmpty()
22+
@MaxLength(STRING_MAX_LENGTH)
2023
@IsIn(['email', 'phone_number', 'username'], {
2124
message: 'Type must be one of: email, phone_number, or username',
2225
})
@@ -29,5 +32,6 @@ export class LoginDTO {
2932
minLength: 8,
3033
})
3134
@IsNotEmpty()
35+
@MaxLength(STRING_MAX_LENGTH)
3236
password: string;
3337
}

0 commit comments

Comments
 (0)