-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
48 lines (41 loc) · 1.37 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const { sequelize } = require('./models');
const routes = require('./routes/index');
const cors = require('cors');
const jwt = require('jsonwebtoken');
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use('/api', routes);
app.post('/login', async (req, res) => {
const { email, password } = req.body;
try {
const user = await User.findOne({ where: { email } });
if (!user || !user.validPassword(password)) {
return res.status(401).json({ message: 'Invalid email or password' });
}
const token = jwt.sign({ id: user.id }, process.env.JWT_SECRET, { expiresIn: '1h' }); // Expires in 1 hour
res.json({ token });
} catch (err) {
console.error(err);
res.status(500).json({ message: 'Internal server error' });
}
});
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({
message: 'Something went wrong!',
error: process.env.NODE_ENV === 'development' ? err.message : {}
});
});
const PORT = process.env.PORT || 3000;
sequelize.sync({ force: false })
.then(() => {
console.log('Database connected and synced');
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});