BinCode/routes/auth.js
Asad 52f6af96f7 feat: add Eurda console feature with pause/play execution and improve UX
- implement pause/play execution
- add scroll synch for textarea and pre elements
- optimze css transition duration for smoother animations
2025-01-18 15:34:54 -05:00

43 lines
1.1 KiB
JavaScript

const express = require('express');
const router = express.Router();
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const { getDb } = require('../db/connect');
const { users: userQueries } = require('../db/queries');
const config = require('../config');
const { validateAuth } = require('../middleware/validation');
router.post('/login', validateAuth, async (req, res, next) => {
try {
const { email, password } = req.body;
const db = getDb();
db.get(userQueries.findByEmail, [email], async (err, user) => {
if (err) {
return next(err);
}
if (!user) {
return res.status(401).json({ error: 'Invalid credentials' });
}
const validPassword = await bcrypt.compare(password, user.password);
if (!validPassword) {
return res.status(401).json({ error: 'Invalid credentials' });
}
const token = jwt.sign(
{ userId: user.id },
config.jwt.secret,
{ expiresIn: config.jwt.expiresIn }
);
res.json({ token });
});
} catch (error) {
next(error);
}
});
module.exports = router;