Skip to content

Commit 1241023

Browse files
authored
Merge pull request #115 from import-ai/fix/auth
fix(auth): fix request format
2 parents 4661015 + a6095d3 commit 1241023

File tree

11 files changed

+31
-46
lines changed

11 files changed

+31
-46
lines changed

base.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ services:
6767
- '-H'
6868
- 'Content-Type: application/json'
6969
- '-d'
70-
- '{"username":"username","password":"Passw0rd","password_repeat":"Passw0rd","email":"omnibox@qq.com"}'
70+
- '{"username":"username","password":"Passw0rd","email":"omnibox@qq.com"}'
7171
depends_on:
7272
backend:
7373
condition: service_healthy

src/app/app.module.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import {
33
MiddlewareConsumer,
44
Module,
55
NestModule,
6+
ValidationPipe,
67
} from '@nestjs/common';
7-
import { APP_INTERCEPTOR } from '@nestjs/core';
8+
import { APP_INTERCEPTOR, APP_PIPE } from '@nestjs/core';
89
import { TypeOrmModule } from '@nestjs/typeorm';
910
import { TagModule } from 'omnibox-backend/tag/tag.module';
1011
// import { CacheModule } from '@nestjs/cache-manager';
@@ -45,6 +46,10 @@ export class AppModule implements NestModule {
4546
provide: APP_INTERCEPTOR,
4647
useClass: SnakeCaseInterceptor,
4748
},
49+
{
50+
provide: APP_PIPE,
51+
useClass: ValidationPipe,
52+
},
4853
],
4954
imports: [
5055
ConfigModule.forRoot({

src/auth/auth.controller.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ export class AuthController {
3636
@Body('token') token: string,
3737
@Body('username') username: string,
3838
@Body('password') password: string,
39-
@Body('password_repeat') password_repeat: string,
4039
) {
4140
return await this.authService.signUpConfirm(token, {
4241
username,
4342
password,
44-
password_repeat,
4543
});
4644
}
4745

@@ -56,9 +54,8 @@ export class AuthController {
5654
async resetPassword(
5755
@Body('token') token: string,
5856
@Body('password') password: string,
59-
@Body('password_repeat') password_repeat: string,
6057
) {
61-
return this.authService.resetPassword(token, password, password_repeat);
58+
return this.authService.resetPassword(token, password);
6259
}
6360

6461
@Post('invite')

src/auth/auth.service.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ export class AuthService {
8181
data: {
8282
username: string;
8383
password: string;
84-
password_repeat: string;
8584
},
8685
) {
8786
const payload: SignUpPayloadDto = await this.jwtVerify(token);
@@ -91,13 +90,17 @@ export class AuthService {
9190
'The email is already registered. Please log in directly.',
9291
);
9392
}
93+
if (data.username.length < 2 || data.username.length > 32) {
94+
throw new BadRequestException(
95+
'Username must be between 2 and 32 characters.',
96+
);
97+
}
9498
return await this.dataSource.transaction(async (manager) => {
9599
const user = await this.userService.create(
96100
{
97101
email: payload.email,
98102
username: data.username,
99103
password: data.password,
100-
password_repeat: data.password_repeat,
101104
},
102105
manager,
103106
);
@@ -137,14 +140,7 @@ export class AuthService {
137140
// return { url: mailSendUri };
138141
}
139142

140-
async resetPassword(
141-
token: string,
142-
password: string,
143-
password_repeat: string,
144-
): Promise<void> {
145-
if (password !== password_repeat) {
146-
throw new BadRequestException('The passwords entered do not match.');
147-
}
143+
async resetPassword(token: string, password: string): Promise<void> {
148144
try {
149145
const payload = this.jwtService.verify(token);
150146
const user = await this.userService.find(payload.sub);

src/resources/dto/create-resource.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { ResourceType } from 'omnibox-backend/resources/resources.entity';
1010

1111
export class CreateResourceDto {
1212
@IsString()
13-
@IsNotEmpty()
13+
@IsOptional()
1414
name: string;
1515

1616
@IsString()

src/resources/dto/update-resource.dto.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { ResourceType } from 'omnibox-backend/resources/resources.entity';
1111
export class UpdateResourceDto {
1212
@IsString()
1313
@IsOptional()
14-
@IsNotEmpty()
1514
name?: string;
1615

1716
@IsString()

src/resources/wizard-task/index.service.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { User } from 'omnibox-backend/user/entities/user.entity';
2-
import { Resource, ResourceType } from 'omnibox-backend/resources/resources.entity';
2+
import {
3+
Resource,
4+
ResourceType,
5+
} from 'omnibox-backend/resources/resources.entity';
36
import { Repository } from 'typeorm';
47
import { Task } from 'omnibox-backend/tasks/tasks.entity';
58
import {

src/user/dto/create-user.dto.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ import {
33
IsString,
44
MinLength,
55
MaxLength,
6+
IsNotEmpty,
67
IsStrongPassword,
78
} from 'class-validator';
89

910
export class CreateUserDto {
1011
@IsString()
11-
@MinLength(4)
12+
@MinLength(2)
1213
@MaxLength(32)
1314
username: string;
1415

1516
@IsEmail()
17+
@IsNotEmpty()
1618
email: string;
1719

20+
@IsString()
21+
@IsNotEmpty()
1822
@IsStrongPassword({ minLength: 8 })
1923
password: string;
20-
21-
@IsStrongPassword({ minLength: 8 })
22-
password_repeat: string;
2324
}

src/user/dto/update-user.dto.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,24 @@ import {
44
MinLength,
55
MaxLength,
66
IsOptional,
7-
IsStrongPassword,
87
} from 'class-validator';
98

109
export class UpdateUserDto {
1110
@IsString()
12-
user_id: string;
13-
14-
@IsString()
15-
@MinLength(4)
11+
@MinLength(2)
1612
@MaxLength(32)
1713
@IsOptional()
18-
username: string;
14+
username?: string;
1915

2016
@IsEmail()
2117
@IsOptional()
22-
email: string;
18+
email?: string;
2319

2420
@IsString()
2521
@IsOptional()
26-
code: string;
22+
code?: string;
2723

28-
@IsStrongPassword({ minLength: 8 })
29-
@IsOptional()
30-
password: string;
31-
32-
@IsStrongPassword({ minLength: 8 })
24+
@IsString()
3325
@IsOptional()
34-
password_repeat: string;
26+
password?: string;
3527
}

src/user/user.service.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ export class UserService {
6969
throw new ConflictException('The account already exists');
7070
}
7171

72-
if (account.password !== account.password_repeat) {
73-
throw new ConflictException('Passwords do not match');
74-
}
75-
7672
const hash = await bcrypt.hash(account.password, 10);
7773
const newUser = repo.create({
7874
...account,
@@ -208,10 +204,7 @@ export class UserService {
208204
if (!existUser) {
209205
throw new ConflictException('The account does not exist');
210206
}
211-
if (account.password && account.password_repeat) {
212-
if (account.password !== account.password_repeat) {
213-
throw new ConflictException('Passwords do not match');
214-
}
207+
if (account.password) {
215208
existUser.password = await bcrypt.hash(account.password, 10);
216209
}
217210
if (account.username && existUser.username !== account.username) {

0 commit comments

Comments
 (0)