-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (76 loc) · 2.7 KB
/
index.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const app = express();
const mongoose = require('mongoose');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcrypt');
const userRoutes = require('./routes/UserRoute');
require('dotenv/config');
const Users = require('./models/UserModel');
try {
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
const corsOptions = {
origin: '*',
optionsSuccessStatus: 200,
credentials: true
};
app.use(cors(corsOptions));
app.use("/login", userRoutes);
// Login Endpoint
app.post('/login', async (req, res) => {
const { username, password } = req.body;
// Find the user in the database
const user = await Users.findOne({ username });
if (!user) {
return res.status(400).json({ error: 'User not found' });
console.log(res)
}
// Compare passwords
const validPassword = await bcrypt.compare(password, user.password);
if (!validPassword) {
return res.status(400).json({ error: 'Invalid credentials' });
console.log(res)
}
// Generate JWT token
const token = jwt.sign(
{ userId: user._id, username: user.username },
process.env.JWT_SECRET,
{ expiresIn: '1h' }
);
// Send the token to the client
res.json({ token });
});
// Middleware to authenticate JWT
const authenticateToken = (req, res, next) => {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[1]; // Bearer TOKEN
if (!token) {
return res.status(401).json({ error: 'Access denied. No token provided.' });
}
// Verify the token
jwt.verify(token, process.env.JWT_SECRET, (err, user) => {
if (err) {
return res.status(403).json({ error: 'Invalid token.' });
}
// Add user
req.user = user;
next();
});
};
// DB Connect
const dbOptions = { useNewUrlParser: true, useUnifiedTopology: true }
mongoose.connect(process.env.DB_URI, dbOptions)
.then(() => console.log('Mongo Database Successfully Connected'))
.catch(err => console.log(err));
const port = process.env.PORT;
app.get('/dashboard', authenticateToken, (req, res) => {
res.json({ message: 'Welcome to your dashboard!', user: req.user });
});
const server = app.listen(port, () => {
console.log("Server is running on : ", port);
})
} catch (error) {
console.log(error)
}