Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,6 @@
{
"name": "password",
"in": "query",
"description": "Hashed password",
"required": true,
"style": "form",
"explode": true,
Expand Down Expand Up @@ -3765,8 +3764,9 @@
},
"password": {
"type": "string",
"description": "User's password hashed",
"example": "$2a$10$uSADqxe22jNkrHd8mudiCOQpsWsvC2BW3jG.8.dmSLJxzsJJgpM5S"
"description": "User's password",
"example": "password123",
"writeOnly": true
},
"firstname": {
"type": "string",
Expand Down
8 changes: 7 additions & 1 deletion src/modules/login/login.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable, BadRequestException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { DB } from '../../db/db';
import { User } from '../users/schemas/users.schema';
import * as bcrypt from 'bcryptjs';

@Injectable()
/**
Expand Down Expand Up @@ -49,8 +50,13 @@ export class LoginService extends DB {
throw new BadRequestException();
}

const hashedPassword = bcrypt.hashSync(
password,
`${process.env.SALT_HASH}`,
);

// Compare the hashed password with the stored password
if (password !== user.users[0].password) {
if (hashedPassword !== user.users[0].password) {
// Throw an error if the password does not match
throw new BadRequestException();
}
Expand Down
19 changes: 17 additions & 2 deletions src/modules/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Counter } from '../../shared/interfaces/counter.interface';
import { UsersDto } from './DTO/users.dto';
import { UpdatePasswordDto } from './DTO/updatepassword.dto';
import { UsersUpdateDto } from './DTO/usersupdate.dto';
import * as bcrypt from 'bcryptjs';

@Injectable()
export class UsersService extends DB {
Expand Down Expand Up @@ -100,9 +101,14 @@ export class UsersService extends DB {

body['id'] = counterDoc.sequence_value;

const newUser = {
...body,
password: bcrypt.hashSync(body.password, `${process.env.SALT_HASH}`),
};

return db
.collection('restaurant')
.updateOne({ id: idRestaurant }, { $addToSet: { users: body } });
.updateOne({ id: idRestaurant }, { $addToSet: { users: newUser } });
}

/**
Expand Down Expand Up @@ -162,7 +168,16 @@ export class UsersService extends DB {
id: number,
updatePasswordDto: UpdatePasswordDto,
): Promise<UpdateResult> {
const { oldPassword, newPassword } = updatePasswordDto;
const { oldPassword, newPassword } = {
oldPassword: bcrypt.hashSync(
updatePasswordDto.oldPassword,
`${process.env.SALT_HASH}`,
),
newPassword: bcrypt.hashSync(
updatePasswordDto.newPassword,
`${process.env.SALT_HASH}`,
),
};

const user = await this.findById(restaurantId, id);

Expand Down