diff options
Diffstat (limited to 'backend')
-rw-r--r-- | backend/hash.js | 9 | ||||
-rw-r--r-- | backend/index.js | 11 |
2 files changed, 18 insertions, 2 deletions
diff --git a/backend/hash.js b/backend/hash.js new file mode 100644 index 0000000..e6d3ba5 --- /dev/null +++ b/backend/hash.js @@ -0,0 +1,9 @@ +const bcrypt = require('bcryptjs'); + +const password = 'password123'; +const saltRounds = 10; + +bcrypt.hash(password, saltRounds, function(err, hash) { + if (err) throw err; + console.log('Hashed password:', hash); +}); diff --git a/backend/index.js b/backend/index.js index 9dae733..090296e 100644 --- a/backend/index.js +++ b/backend/index.js @@ -7,6 +7,8 @@ const jwt = require('jsonwebtoken'); const app = express(); const PORT = 5000; +const SECRET_KEY = 'your_jwt_secret'; // Replace with your actual secret key + // Middleware app.use(bodyParser.json()); app.use(cors()); @@ -16,25 +18,30 @@ const users = [ { id: 1, username: 'admin', - password: '$2a$10$QWJkLkFgD1kz6X.0Q1jDg.2aH3eRJ/Qnl72sDgB5DlQvPvFjsKFDi', // hashed password for 'password123' + password: '$2a$10$0chEQ/BjpmW2W9J1aA/BNOwF0aeFSBg4IAXnPjjLzSnQmXagdIzra', // hashed password for 'password123' }, ]; // Login route app.post('/login', async (req, res) => { const { username, password } = req.body; + console.log('Login attempt:', username, password); const user = users.find((user) => user.username === username); if (!user) { + console.log('User not found'); return res.status(401).json({ message: 'Invalid credentials' }); } const isPasswordValid = await bcrypt.compare(password, user.password); + console.log('Password valid:', isPasswordValid); if (!isPasswordValid) { + console.log('Invalid password'); return res.status(401).json({ message: 'Invalid credentials' }); } - const token = jwt.sign({ id: user.id }, 'your_jwt_secret', { expiresIn: '1h' }); + const token = jwt.sign({ id: user.id }, SECRET_KEY, { expiresIn: '1h' }); + console.log('Token generated:', token); res.json({ token }); }); |