11import { Router } from 'express' ;
22import bcrypt from 'bcrypt' ;
3+ import argon2 from 'argon2' ;
34import config from '../../config.js' ;
45import 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