-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (77 loc) · 2.36 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
const express = require('express');
const http = require('http');
const socketIo = require('socket.io');
const cors = require('cors');
const jwt = require('jsonwebtoken');
const bcrypt = require('bcrypt');
const { MongoClient } = require('mongodb');
const mongoose = require('mongoose');
const app = express();
app.use(express.json());
app.use(cors());
const SECRET = 'SECr3t';
const userSchema = new mongoose.Schema({
username: String,
password: String,
});
const User = mongoose.model('User', userSchema);
const authenticateJwt = (req, res, next) => {
const authHeader = req.headers.authorization;
if (authHeader) {
const token = authHeader.split(' ')[1];
jwt.verify(token, SECRET, (err, user) => {
if (err) {
return res.sendStatus(403);
}
req.user = user;
next();
});
} else {
res.sendStatus(401);
}
};
mongoose.connect('mongodb+srv://ropz123:<password>@atlascluster.n1dl8ny.mongodb.net/test', { useNewUrlParser: true, useUnifiedTopology: true, dbName: "test" });
// register route
app.post('/register', async (req, res) => {
const { username, password } = req.body;
const user = await User.findOne({ username });
if (user) {
res.status(403).json({ message: 'User already exists' });
} else {
const newUser = new User({ username, password });
await newUser.save();
const token = jwt.sign({ username, role: 'user' }, SECRET, { expiresIn: '1h' });
res.json({ message: 'User created successfully', token });
}
});
// login route
app.post('/login', async (req, res) => {
const { username, password } = req.headers;
const user = await User.findOne({ username, password });
if (user) {
const token = jwt.sign({ username, role: 'user' }, SECRET, { expiresIn: '1h' });
res.json({ message: 'Logged in successfully', token });
} else {
res.status(403).json({ message: 'Invalid username or password' } );
}
});
// connecting to socket.io
const server = http.createServer(app);
const io = socketIo(server, {
cors: {
origin: '*',
}
});
io.on('connection', (socket) => {
console.log('New user connected');
socket.on('sendMessage', (message) => {
io.emit('message', message);
});
socket.on('disconnect', () => {
console.log('User disconnected');
});
});
const PORT = process.env.PORT || 5001;
server.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});