-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
149 lines (131 loc) · 3.44 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const express = require('express');
var app = express();
const path = require('path');
const socketio = require("socket.io")
const passport = require("passport");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");
app.use(cors());
var User = require("./models/User");
var Chat = require("./models/Chat");
var Message = require("./models/Message");
app.set("view engine","ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname + "/public")));
//DqfAOzgQadYFYuIL
mongoose.connect("mongodb+srv://Harshit:[email protected]/TextChatApp?retryWrites=true&w=majority")
require("./passport-setup.js");
app.use(
require("express-session")({
secret: "chatapp",
resave: false,
saveUninitialized: false,
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(function (req, res, next) {
res.locals.currUser = req.user;
next();
});
const isLoggedIn = (req, res, next) => {
if (req.user) {
next();
} else {
res.redirect("/");
}
};
app.get(
"/login",
passport.authenticate("google", { scope: ["profile", "email"] })
);
app.get(
"/google/callback",
passport.authenticate("google", { failureRedirect: "/failed" }),
function (req, res) {
// Successful authentication, redirect home.
res.redirect("/home");
}
);
app.get("/failed", (req, res) => {
res.redirect("/");
});
app.get("/",(req,res)=>{
res.render("index");
});
app.get("/home",isLoggedIn,async(req,res)=>{
var u = await User.findById(req.user._id).populate("chats");
res.render("main",{u});
});
//=======================================
//CHATS
app.get("/msg/:chatid",isLoggedIn,async(req,res)=>{
var c = await Chat.findById(req.params.chatid).populate("messages");
res.render("chatscreen",{c});
})
//=========================================
app.get("/new",isLoggedIn,async(req,res)=>{
var u = await User.find();
res.render("newpage",{u});
});
app.get("/new/:id",isLoggedIn,async(req,res)=>{
var u = await User.findById(req.params.id);
var u2 = await User.findById(req.user._id);
var c = {
a1:{
name:u.name,
pic:u.google.picture,
id:u._id
},
a2:{
name:u2.name,
pic:u2.google.picture,
id:u2._id
},
messages:[],
timestamp:Date.now()
}
var rc = new Chat(c);
rc.save();
u.chats.push(rc);
u.save();
u2.chats.push(rc);
u2.save();
res.redirect("/home");
});
//=======================================
app.get("/profile",isLoggedIn,async(req,res)=>{
var u = await User.findById(req.user._id);
res.render("profile",{u});
});
app.get("/logout", (req, res) => {
req.logout();
res.redirect("/");
req.session.destroy();
});
var port = process.env.PORT || 3000;
const exserver= app.listen(port);
const io = socketio(exserver);
io.on("connection", function(socket){
// socket.emit('messageFromServer',{data:"Welcome to server"});
socket.on('messageToServer',async(dfromC)=>{
var u = await User.findById(dfromC.uid);
var c = await Chat.findById(dfromC.cid);
var m = {
author:{
name:u.name,
email:u.email,
id:u._id
},
content:dfromC.content
}
var msg = new Message(m);
msg.save();
c.messages.push(msg);
c.timestamp = Date.now();
c.save();
console.log(socket.id);
io.to(dfromC.s).emit('messageFromServer',{msg});
})
});