Skip to content

Commit 1a544f7

Browse files
Merge pull request #6 from neuralarchitects/bcrypt
1 - Moved the password hashing process from frontend to backend. 2- Fixed the user registration process by checking if the user already exist. fix: remove bcrypt from webapp, delegate password hashing to back-end #5
2 parents ff77d14 + 25fb6ac commit 1a544f7

File tree

7 files changed

+56
-62
lines changed

7 files changed

+56
-62
lines changed

backend/src/modules/user/services/user/user.service.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Inject, Injectable, forwardRef } from '@nestjs/common';
1+
import { Inject, Injectable, forwardRef, Logger } from '@nestjs/common';
22
import { Types } from 'mongoose';
33
import { ErrorTypeEnum } from 'src/modules/utility/enums/error-type.enum';
44
import { OTPTypeEnum } from 'src/modules/utility/enums/otp-type.enum';
@@ -36,7 +36,7 @@ import {
3636
import { MailService } from 'src/modules/utility/services/mail.service';
3737
import { randomBytes } from 'crypto';
3838

39-
const saltRounds = process.env.CRYPTION_SALT;
39+
const saltRounds = parseInt(process.env.CRYPTION_SALT) || 10;
4040

4141
/**
4242
* User manipulation service.
@@ -249,14 +249,13 @@ export class UserService {
249249
}
250250
} catch (error) {}
251251

252-
this.otp = await this.otpService.findOTPByEmail(
252+
const otp = await this.otpService.findOTPByEmail(
253253
body.email,
254254
OTPTypeEnum.REGISTRATION,
255255
);
256-
257256
if (
258-
this.otp.length == 0 ||
259-
new Date(this.otp[this.otp.length - 1].expiryDate).getTime() <
257+
otp.length == 0 ||
258+
new Date(otp[otp.length - 1].expiryDate).getTime() <
260259
new Date().getTime()
261260
) {
262261
/* const StorX = await storxController.createUserAndGenerateStorXKey(
@@ -1966,6 +1965,10 @@ export class UserService {
19661965
roles.push(ordinaryUserRole);
19671966
}
19681967

1968+
if (body.password) {
1969+
body.password = await bcrypt.hash(String(body.password), saltRounds);
1970+
}
1971+
19691972
const newUser = {
19701973
...body,
19711974
StorX: body.StorX || {},

backend/src/modules/utility/services/mail.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ export class MailService {
194194
return false;
195195
} */
196196

197-
const userToken = await this.getTokenWithUserEmail(email);
197+
198+
// const userToken = await this.getTokenWithUserEmail(email);
198199

199200
await this.mailerService
200201
.sendMail({
@@ -206,7 +207,7 @@ export class MailService {
206207
NodeName: process.env.NODE_NAME,
207208
NodeImageSrc: process.env.THEME_LOGO,
208209
url: url,
209-
unsubscribeEmailUrl: `${this.validateTokenUrl}${userToken}`,
210+
// unsubscribeEmailUrl: `${this.validateTokenUrl}${userToken}`,
210211
},
211212
attachments: [
212213
{

package-lock.json

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

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"devDependencies": {
3+
"@types/bcrypt": "^6.0.0"
4+
}
5+
}

web_app/Source_webapp/package-lock.json

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

web_app/Source_webapp/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
"@visx/pattern": "^3.0.0",
3636
"apexcharts": "^3.37.3",
3737
"axios": "^1.3.4",
38-
"bcrypt": "^6.0.0",
3938
"blockly": "^10.4.3",
4039
"classnames": "^2.3.2",
4140
"d3-dsv": "^3.0.1",

web_app/Source_webapp/src/utils/hooks/useAuth.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ import { REDIRECT_URL_KEY } from '@/constants/app.constant'
1212
import { useNavigate } from 'react-router-dom'
1313
import useQuery from './useQuery'
1414
import type { SignInCredential, SignUpCredential } from '@/@types/auth'
15-
import * as bcrypt from 'bcrypt'
1615

1716
type Status = 'success' | 'failed'
1817

1918
function useAuth() {
2019
const dispatch = useAppDispatch()
21-
const saltRounds = parseInt(process.env.CRYPTION_SALT || '10', 10)
2220
const navigate = useNavigate()
2321

2422
const query = useQuery()
@@ -37,14 +35,6 @@ function useAuth() {
3735
try {
3836
let resp
3937

40-
const salt = bcrypt.genSaltSync(saltRounds)
41-
const hashedNewPassword = bcrypt.hashSync(
42-
String(values.password),
43-
salt
44-
)
45-
46-
values = { ...values, password: hashedNewPassword }
47-
4838
if (values.tokenId || values.accessToken) {
4939
try {
5040
resp = await apiSignInGoogle(
@@ -98,14 +88,6 @@ function useAuth() {
9888

9989
const signUp = async (values: SignUpCredential) => {
10090
try {
101-
const salt = bcrypt.genSaltSync(saltRounds)
102-
const hashedNewPassword = bcrypt.hashSync(
103-
String(values.password),
104-
salt
105-
)
106-
107-
values = { ...values, password: hashedNewPassword }
108-
10991
const resp = await apiSignUp(values)
11092
console.log(resp)
11193
if (resp.data) {

0 commit comments

Comments
 (0)