-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
151 lines (129 loc) · 3.73 KB
/
server.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
150
151
const express = require("express");
const app = express();
const port = process.env.PORT || 5000;
const server = require("http").Server(app);
const io = require("socket.io")(server);
const cookieParser = require("cookie-parser");
//Authentication with Google Auth
const { OAuth2Client } = require("google-auth-library");
const CLIENT_ID =
"36522074585-bs3s7dt60b20l7na8pmp2r2dekh2smfo.apps.googleusercontent.com";
const client = new OAuth2Client(CLIENT_ID);
//Import peerjs
const { ExpressPeerServer } = require("peer");
const peerServer = ExpressPeerServer(server, {
debug: true,
});
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use("/peerjs", peerServer);
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(cookieParser());
//rendering the index first
app.get("/", (req, res) => {
res.render("login");
});
app.post("/login", (req, res) => {
let token = req.body.token;
async function verify() {
const ticket = await client.verifyIdToken({
idToken: token,
audience: CLIENT_ID,
});
const payload = ticket.getPayload();
const userid = payload["sub"];
}
verify()
.then(() => {
res.cookie("session-token", token);
res.send("success");
})
.catch(console.error);
});
app.get("/index", (req, res) => {
res.render("index");
});
//posting the values from index and redirecting to dashboard
app.post("/index", checkAuthenticated, (req, res) => {
roomname = req.body.roomname;
res.redirect(`/${roomname}`);
});
app.get("/:num", checkAuthenticated, (req, res) => {
let user = req.user;
res.render("dashboard", { dashID: req.params.num, user });
});
//Function to check if authenticated
function checkAuthenticated(req, res, next) {
let token = req.cookies["session-token"];
let user = {};
async function verify() {
const ticket = await client.verifyIdToken({
idToken: token,
audience: CLIENT_ID,
});
const payload = ticket.getPayload();
user.name = payload.name;
user.email = payload.email;
user.picture = payload.picture;
}
verify()
.then(() => {
req.user = user;
next();
})
.catch((err) => {
res.redirect("/");
});
}
const users = {};
//Funtion to get users online in a room
function getUsers(arr) {
onlineUsers = [];
arr.forEach((onlineUser) => {
onlineUsers.push(Object.values(onlineUser)[0]);
});
return onlineUsers;
}
io.on("connection", (socket) => {
socket.on("join-room", (dashID, userID, userName, userImage) => {
var user = {};
//Joining the socket room
socket.join(dashID);
//storing users connected in a room
user[socket.id] = [userName, userImage];
if (users[dashID]) {
users[dashID].push(user);
} else {
users[dashID] = [user];
}
//Emitting username to client
socket.to(dashID).emit("user-connected", userID, userName);
//Send online users array
io.to(dashID).emit("online-users", getUsers(users[dashID]));
//Emitting chat message
socket.on("chat-message", (message, userName, userImage) => {
io.to(dashID).emit("chat-message", {
message: message,
name: userName,
image: userImage,
});
});
//Emit username when user raised hand
socket.on("raiseHand", (userName) => {
io.to(dashID).emit("handRaised", userName);
});
//Remove user from memory when they disconnect
socket.on("disconnect", () => {
socket.to(dashID).emit("user-disconnected", userID);
users[dashID].forEach((user, index) => {
if (user[socket.id]) {
users[dashID].splice(index, 1);
}
//Send online users array
io.to(dashID).emit("online-users", getUsers(users[dashID]));
});
});
});
});
server.listen(port);