Skip to content

Commit 1a5832b

Browse files
committed
Implement password verification using Argon2 and Bcrypt in login route
1 parent 698fb8e commit 1a5832b

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

src/server/routes/auth.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Router } from 'express';
22
import bcrypt from 'bcrypt';
3+
import argon2 from 'argon2';
34
import config from '../../config.js';
45
import logger from '../../utils/logger.js';
56

@@ -14,9 +15,29 @@ router.get("/login", (req, res) => {
1415
});
1516

1617
// Login route - POST
17-
router.post("/login", (req, res) => {
18+
router.post("/login", async (req, res) => {
1819
const { username, password } = req.body;
19-
if (username === config.server_username && bcrypt.compareSync(password, config.server_password)) {
20+
21+
let isPasswordMatch = false;
22+
23+
// Check if the stored password is a hash or plain text
24+
if (config.server_password.startsWith('$argon2')) {
25+
// Argon2 hash
26+
try {
27+
isPasswordMatch = await argon2.verify(config.server_password, password);
28+
} catch (err) {
29+
logger.error("Error verifying argon2 password:", err);
30+
isPasswordMatch = false;
31+
}
32+
} else if (config.server_password.startsWith('$2')) {
33+
// Bcrypt hash
34+
isPasswordMatch = await bcrypt.compare(password, config.server_password);
35+
} else {
36+
// Plain text (not recommended)
37+
isPasswordMatch = password === config.server_password;
38+
}
39+
40+
if (username === config.server_username && isPasswordMatch) {
2041
(req.session as { user?: unknown }).user = username;
2142
res.redirect("/");
2243
} else {

0 commit comments

Comments
 (0)