2023-10-25 23:02:31 +05:30

37 lines
1.1 KiB
JavaScript

const express = require('express');
const bodyParser = require('body-parser');
const jwt = require('jsonwebtoken'); // Import the jsonwebtoken library
const app = express();
const PORT = 5000;
const cors = require('cors');
const corsOptions = {
origin: 'http://127.0.0.1:5500', // Replace with your frontend's origin
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
credentials: true,
optionsSuccessStatus: 204,
};
app.use(cors(corsOptions));
app.use(bodyParser.json());
const secretKey = 'your-secret-key';
app.post('/api/auth', (req, res) => {
const { user_name, password } = req.body;
// Check the username and password (for demonstration purposes, accept any non-empty values)
if (user_name==="Yash" && password==="123") {
// Authentication successful, create a JWT token
const token = jwt.sign({ user_name }, secretKey, { expiresIn: '1h' }); // Token expires in 1 hour
res.json({ token });
} else {
// Authentication failed
res.status(401).json({ error: 'Invalid username or password' });
}
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});