Skip to content

Commit 18d21e0

Browse files
committed
feat : add server side password verification
1 parent 2fc4342 commit 18d21e0

File tree

4 files changed

+36
-14
lines changed

4 files changed

+36
-14
lines changed

Backend/controllers/auth.js

+6-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import jwt from 'jsonwebtoken';
88

99
env.config();
1010

11-
const saltRounds = 10;
1211
const __filename = fileURLToPath(import.meta.url);
1312
const __dirname = path.dirname(__filename);
1413
const views_path = path.join(__dirname, "..", "views");
@@ -31,7 +30,7 @@ async function verifyMail(email,token) {
3130
from: '"AO3" <[email protected]>', // sender address
3231
to: email, // user email address
3332
subject: "Conform Your Mail Account", // Subject line
34-
html: `to activate your account please follow the link <b><a>${link}</a></b> you will be redirected to AO3 website after this </br> <b>Note : this link will expire in one hour</b>`, // html body
33+
html: `to activate your account please follow the link <a><b>${link}</b></a> you will be redirected to AO3 website after this </br> <b>Note : this link will expire in one hour</b>`, // html body
3534
});
3635
}
3736

@@ -49,7 +48,7 @@ export const post_login = (req, res) => {
4948
return;
5049
}
5150
if (result.rows.length === 0) {
52-
res.status(401).send("Invalid credentials");
51+
res.status(401).send("User does not exist");
5352
return;
5453
}
5554
const user = result.rows[0];
@@ -62,6 +61,7 @@ export const post_login = (req, res) => {
6261
res.send("authSuccess");
6362
res.redirect("/dashboard");
6463
});
64+
db.end();
6565
};
6666

6767
export const post_register = (req, res) => {
@@ -79,32 +79,25 @@ export const post_register = (req, res) => {
7979
res.status(409).send("Email already exists");
8080
return;
8181
}
82-
// if he dosen't Store in database
82+
// if user dosen't exist
8383
else{
84-
bcrypt.hash(password, saltRounds, (err, hashedPassword) => {
85-
if (err) {
86-
console.error(err);
87-
res.status(500).send("Server error");
88-
return;
89-
}
9084
db.query(
9185
"INSERT INTO login (username,email,password) VALUES($1,$2,$3)",
92-
[username, email, hashedPassword],
86+
[username, email, password],
9387
(err, result) => {
9488
if (err) {
9589
console.error(err);
9690
res.status(500).send("Server error");
9791
return;
9892
}
99-
10093
res.send("Registration successful");
10194
// Node mailer will send mail to the user
10295
verifyMail(email,jwt.sign({'email':email},process.env.ACESS_TOKEN_SECRET,{expiresIn:'1h'}));
10396
}
10497
);
105-
});
10698
}
10799
});
100+
db.end();
108101
}
109102

110103
export const get_token = (req,res) => {

Backend/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const app = express();
1111
// middleware
1212
app.use(bodyParser.urlencoded({ extended: true }));
1313
app.use("/auth", auth);
14+
app.use(express.json());
1415

1516
//the following will be redirected to the dashboard for now to the login page
1617
app.get("/", (req, res) => {

Backend/middelware/auth_middleware.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import bcrypt from 'bcrypt';
2+
3+
const generateHash = async (password) => {
4+
const salt = await bcrypt.genSalt();
5+
return await bcrypt.hash(password, salt);
6+
};
7+
8+
//regex pattern matching
9+
export const hashPassword = async (req, res, next) => {
10+
const password = req.body.password;
11+
const pattern = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,20}$/;
12+
if (!pattern.test(password)) {
13+
return res.status(400).json({
14+
success: false,
15+
message: {
16+
type: "Weak Password",
17+
content:
18+
"Password must be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, and one number.",
19+
},
20+
});
21+
}
22+
req.body.password = await generateHash(password);
23+
next();
24+
};
25+
26+
27+

Backend/routes/auth.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import express from "express";
22
import * as func from "../controllers/auth.js";
3+
import { hashPassword } from "../middelware/auth_middleware.js";
34

45
const router = express.Router();
56

67
//login routes
78
router.get("/login", func.get_login);
89
router.post("/login", func.post_login);
9-
router.post("/register", func.post_register);
10+
router.post("/register", hashPassword ,func.post_register);
1011
router.get("/token/:token", func.get_token);
1112

1213
export default router;

0 commit comments

Comments
 (0)