aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--backend/hash.js9
-rw-r--r--backend/index.js11
-rw-r--r--src/components/Login.js7
3 files changed, 23 insertions, 4 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 });
});
diff --git a/src/components/Login.js b/src/components/Login.js
index 1b0a9ed..c73b59a 100644
--- a/src/components/Login.js
+++ b/src/components/Login.js
@@ -9,12 +9,15 @@ function Login() {
const handleSubmit = async (event) => {
event.preventDefault();
try {
+ console.log('Attempting login with:', { username, password });
const response = await axios.post('http://localhost:5000/login', {
username,
password,
});
- console.log(response.data);
- // handle successful login, e.g., redirect to admin panel
+ const { token } = response.data;
+ console.log('Login successful, token:', token);
+ localStorage.setItem('jwtToken', token);
+ // Redirect to admin panel or another page after successful login
} catch (error) {
setError('Invalid credentials');
console.error('Error logging in', error);