diff options
author | Biswakalyan Bhuyan <biswa@surgot.in> | 2024-07-25 12:09:28 +0530 |
---|---|---|
committer | Biswakalyan Bhuyan <biswa@surgot.in> | 2024-07-25 12:09:28 +0530 |
commit | c00ac1ff51c795d4d93c32e0a913e2cebf917d0c (patch) | |
tree | 0b5f59a0167b6169426648c383082dbbb3b4f2c7 /backend/index.js | |
parent | c796d53d2f2391e4f4a075b243cc2c50db38d7da (diff) | |
download | admin-panel-c00ac1ff51c795d4d93c32e0a913e2cebf917d0c.tar.gz admin-panel-c00ac1ff51c795d4d93c32e0a913e2cebf917d0c.tar.bz2 admin-panel-c00ac1ff51c795d4d93c32e0a913e2cebf917d0c.zip |
added backend and login portal
Diffstat (limited to 'backend/index.js')
-rw-r--r-- | backend/index.js | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/backend/index.js b/backend/index.js new file mode 100644 index 0000000..670927f --- /dev/null +++ b/backend/index.js @@ -0,0 +1,43 @@ +const expres = require('express'); +const bodyParse = require('body-parser'); +const cors = require('cors'); +const bcrypt = require('bcryptjs'); +const jwt = require('jsonwebtoken'); + +const app = express(); +const PORT = 5000; + +// middleware +app.use(bodyParser.json()); +app.use(cors()); + +// dummy user data +const user = [ + { + id: 1, + username: 'admin', + password: '$2a$10$QWJkLkFgD1kz6X.0Q1jDg.2aH3eRJ/Qnl72sDgB5DlQvPvFjsKFDi' //hashed password for 'password123' + }, +]; + +// login route +app.post('/login', async (req, res) => { + const { username, password } = req.body; + + const user = users.find((user) => user.username === username); + if (!user) { + return res.status(401).json({ message: 'Invalid credentials'}); + } + + const isPasswordValid = await bcrypt.compare(password, user.password); + if (!isPasswordValid) { + return res.status(401).json({ message: 'Invalid credentials'}); + } + + const token = jwt.sign({ id: user.id }, 'your_jwt_secret', { expiresIn: '1h' }); + res.json({ token }); +}); + +app.listen(PORT, () => { + console.log(`Server running on http://localhost:${PORT}`); +});
\ No newline at end of file |