Skip to content

Commit

Permalink
fix(user): added checking old password
Browse files Browse the repository at this point in the history
  • Loading branch information
Shchepotin committed Mar 2, 2021
1 parent 8e2334a commit e09d28d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/docker-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: NestJS API CI

on:
push:
branches: []
branches: [master]
pull_request:
branches: [master]

Expand All @@ -12,8 +12,5 @@ jobs:

steps:
- uses: actions/checkout@v2
- run: docker-compose -f docker-compose.ci.yaml --env-file env-example -p ci pull
- uses: satackey/action-docker-layer-caching@v0.0.11
continue-on-error: true
- name: Run e2e tests
run: docker-compose -f docker-compose.ci.yaml --env-file env-example -p ci up --build --exit-code-from api
33 changes: 33 additions & 0 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,39 @@ export class AuthService {
}

async update(user: User, userDto: AuthUpdateDto): Promise<User> {
if (userDto.password) {
if (userDto.oldPassword) {
const currentUser = await this.usersRepository.findOne(user.id);

const isValidOldPassword = await bcrypt.compare(
userDto.oldPassword,
currentUser.password,
);

if (!isValidOldPassword) {
throw new HttpException(
{
status: HttpStatus.UNPROCESSABLE_ENTITY,
errors: {
oldPassword: 'incorrectOldPassword',
},
},
HttpStatus.UNPROCESSABLE_ENTITY,
);
}
} else {
throw new HttpException(
{
status: HttpStatus.UNPROCESSABLE_ENTITY,
errors: {
oldPassword: 'missingOldPassword',
},
},
HttpStatus.UNPROCESSABLE_ENTITY,
);
}
}

await this.usersRepository.save(
this.usersRepository.create({
id: user.id,
Expand Down
8 changes: 6 additions & 2 deletions src/auth/dtos/auth-update.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ export class AuthUpdateDto {
photo?: FileEntity;

@ApiProperty({ example: 'John' })
@IsNotEmpty()
@IsNotEmpty({ message: 'mustBeNotEmpty' })
firstName?: string;

@ApiProperty({ example: 'Doe' })
@IsNotEmpty()
@IsNotEmpty({ message: 'mustBeNotEmpty' })
lastName?: string;

@ApiProperty()
@IsNotEmpty()
@MinLength(6)
password?: string;

@ApiProperty()
@IsNotEmpty({ message: 'mustBeNotEmpty' })
oldPassword: string;
}
17 changes: 15 additions & 2 deletions test/user/auth.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ describe('Auth user (e2e)', () => {
.send({ email: newUserEmail, password: newUserPassword })
.then(({ body }) => body.token);

await request(app)
.patch('/api/v1/auth/me')
.auth(newUserApiToken, {
type: 'bearer',
})
.send({
firstName: newUserNewName,
password: newUserNewPassword,
})
.expect(422);

await request(app)
.patch('/api/v1/auth/me')
.auth(newUserApiToken, {
Expand All @@ -118,7 +129,8 @@ describe('Auth user (e2e)', () => {
firstName: newUserNewName,
password: newUserNewPassword,
oldPassword: newUserPassword,
});
})
.expect(200);

await request(app)
.post('/api/v1/auth/login/email')
Expand All @@ -133,7 +145,8 @@ describe('Auth user (e2e)', () => {
.auth(newUserApiToken, {
type: 'bearer',
})
.send({ password: newUserPassword, oldPassword: newUserNewPassword });
.send({ password: newUserPassword, oldPassword: newUserNewPassword })
.expect(200);
});

it('New user delete profile: /api/v1/auth/me (DELETE)', async () => {
Expand Down

0 comments on commit e09d28d

Please sign in to comment.