cors added + session working
This commit is contained in:
parent
dff5bd01e0
commit
11e51a80db
1 changed files with 18 additions and 14 deletions
|
|
@ -8,22 +8,22 @@ const bodyParser = require ('body-parser');
|
||||||
const {sendError, sendMessage} = require ('./message');
|
const {sendError, sendMessage} = require ('./message');
|
||||||
const messages = require('./mongodb-message');
|
const messages = require('./mongodb-message');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const server = http.createServer(app);
|
const server = http.createServer(app);
|
||||||
const io = new Server(server);
|
const io = new Server(server, {
|
||||||
const port = process.env.PORT || 3000;
|
cors: {
|
||||||
|
origin: "http://127.0.0.1:4200",
|
||||||
|
methods: ["GET", "POST"],
|
||||||
|
credentials: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const port = process.env.PORT || 3001;
|
||||||
|
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
app.use(cors({origin: 'http://127.0.0.1:4200', credentials: true}));
|
app.use(cors({origin: 'http://127.0.0.1:4200', credentials: true}));
|
||||||
app.use(cookieParser());
|
app.use(cookieParser());
|
||||||
io.use(function(socket, next){
|
|
||||||
const session = auth.getSession(socket.request);
|
|
||||||
const getUsername = auth.getUsername(session);
|
|
||||||
if (getUsername === -1) {
|
|
||||||
//sendError(res, 'not authenticated');
|
|
||||||
}
|
|
||||||
auth.setSessionCookie(socket.request, socket.request.res || {}, next);
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get('/', (req, res) => {
|
app.get('/', (req, res) => {
|
||||||
res.sendFile(__dirname + '/index.html');
|
res.sendFile(__dirname + '/index.html');
|
||||||
|
|
@ -34,20 +34,24 @@ io.on('connection',socket => {
|
||||||
|
|
||||||
const session = auth.getSession(socket.request);
|
const session = auth.getSession(socket.request);
|
||||||
const getUsername = auth.getUsername(session);
|
const getUsername = auth.getUsername(session);
|
||||||
|
if (getUsername === -1) {
|
||||||
|
return sendError(socket, 'not authenticated');
|
||||||
|
}
|
||||||
|
|
||||||
console.log(`${getUsername} joined the chat.`);
|
console.log(`${getUsername} joined the chat.`);
|
||||||
socket.broadcast.emit('general',`${getUsername} joined the chat.`);
|
socket.emit('general',`${getUsername} joined the chat.`);
|
||||||
users[socket.id] = getUsername;
|
users[socket.id] = getUsername;
|
||||||
messages.find({},(err, res) => {
|
messages.find({},(err, res) => {
|
||||||
if(err) throw err;
|
if(err) throw err;
|
||||||
if(res.length > 0){
|
if(res.length > 0){
|
||||||
const savedChat = res;
|
const savedChat = res;
|
||||||
|
console.log(res);
|
||||||
socket.emit('general',savedChat);
|
socket.emit('general',savedChat);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('general',function(data){
|
socket.on('general',function(data){
|
||||||
socket.broadcast.emit('general',data);
|
socket.emit('general',data);
|
||||||
|
|
||||||
const username = data.username;
|
const username = data.username;
|
||||||
const date = Date.now();
|
const date = Date.now();
|
||||||
|
|
@ -73,11 +77,11 @@ io.on('connection',socket => {
|
||||||
})
|
})
|
||||||
|
|
||||||
socket.on("disconnect", function() {
|
socket.on("disconnect", function() {
|
||||||
console.log(`${socket.id} left the chat.`);
|
console.log(`${getUsername} left the chat.`);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen(port, () => {
|
server.listen(port, () => {
|
||||||
console.log('listening on *:3000');
|
console.log(`listening on *:${port}/`);
|
||||||
});
|
});
|
||||||
Reference in a new issue