-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (101 loc) · 2.92 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const PORT = 1003;
const express = require('express');
const cookieParser = require('cookie-parser');
const passport= require('passport');
const passportLocal = require('./config/passport-local');
const path = require('path');
const app = express();
const session = require('express-session');
app.set('view engine','ejs');
app.use(express.static('./assets'));
app.set('views',path.join(__dirname,'views'));
const database=require('./config/mongoose');
const ChatUser = require('./models/chatUser');
const MongoStore = require('connect-mongo');
const server = require('http').createServer(app);
const io= require('socket.io')(server,{cors:{origin:'*'}})
app.use(express.urlencoded({extended:true}));
app.use(cookieParser()); //middleware
app.use(session({
name: 'login',
secret:'xyz',
saveUninitialized : false,
resave : false,
cookie: {
maxAge: (100 * 60 * 1000)
},
//connecting mongo store
store : MongoStore.create({
mongoUrl:'mongodb://localhost:27017/login',
mongooseConnect: database,
autoRemove : 'disable' //session can't be removed automatically
})
}))
app.use(passport.initialize());
app.use(passport.session());
app.use(passport.setAuthenticatedUser);
app.get('/',function(req,res){
return res.render('home');
})
app.get('/login',function(req,res){
return res.render('login');
})
app.post('/userLogin',
passport.authenticate(
'local',
{failureRedirect: '/signup'}
),function(req,res){
return res.redirect('/chatRoom')
})
app.get('/signup',function(req,res){
if(req.isAuthenticated()){
return res.redirect('/chatRoom');
}
//console.log(req.cookies);
return res.render('signup');
})
app.post('/userCreate',function(req,res){
if(req.body.password != req.body.confirm_password){
console.log("Password not matched");
return res.redirect('back');
}
ChatUser.findOne({email : req.body.email},function(err,user){
if(err){
console.log("error found");
return;
}
if(!user){
ChatUser.create(req.body,function(err,user){
console.log(req.body);
if(err){
console.log("error found");
return;
}
return res.redirect('/login')
})
}
else{
return res.redirect('back');
}
})
})
app.get('/chatRoom',passport.checkAuthentication,function(req,res){
return res.render('chat_box');
})
app.get('/logout',function(req,res){
req.logOut();
return res.redirect('/signup');
})
server.listen(PORT,function(err){
if(err){
console.log(err);
return;
}
console.log("Server is running on port",PORT)
})
io.on('connection',(socket)=>{
console.log('Token',socket.id);
socket.on('message',(data)=>{
socket.broadcast.emit('message',data);
})
})