forked from cs4241-20a/final-project
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
152 lines (128 loc) · 4.21 KB
/
server.js
File metadata and controls
152 lines (128 loc) · 4.21 KB
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
152
const express = require("express");
const http = require("http");
const socketIO = require("socket.io");
const fetch = require("node-fetch");
const cookieSession = require("cookie-session");
const { request, response } = require("express");
const dotenv = require("dotenv").config(); //is this necessary?
const bodyparser = require('body-parser');
const app = express();
app.use(express.json()); // body-parser
app.use( cookieSession({
name: 'Pictocookies',
keys: ['key1'],
secret: process.env.COOKIE_SECRET
}));
const server = http.createServer(app); // required for socket io
const io = socketIO(server);
io.on("connection", (client) => {
console.log("New client connected", client.id);
client.on("join", (roomID) => {
console.log(client.id, "joined", roomID);
client.join(roomID);
io.sockets.in(roomID).emit("chat", "New User Joined");
});
client.on("chat", (msg) => {
io.sockets.in(msg.roomID).emit("chat", msg.message);
});
client.on("disconnect", () => {
console.log("Client disconnected", client.id);
});
});
//GITHUB LOGIN STUFF
app.get('/getghurl', (req, res) => {
const path = req.protocol + '://' + req.get('host');
const ghid = process.env.GHID;
const url = `https://github.com/login/oauth/authorize?client_id=${ghid}&redirect_uri=${path}/login/github/callback`;
res.json(url);
})
async function getGHAccessToken(code, ghid, ghsecret) {
const response = await fetch('https://github.com/login/oauth/access_token', {
method: 'POST',
headers: {"Content-Type": "application/json"},
body: JSON.stringify({
client_id: ghid,
client_secret: ghsecret,
code
})
})
.then( response => {
//console.log(response)
response = response.text();
return response;
} );
const params = new URLSearchParams(response);
return params.get('access_token');
}
async function getGHUser(accessToken) {
const ghdata = await fetch('https://api.github.com/user', {
headers: { Authorization: `bearer ${accessToken}`}
})
.then (ghdata => ghdata.json() );
return ghdata;
}
app.get('/login/github/callback', async (req, res) => {
const code = req.query.code;
const ghid = process.env.GHID;
const ghsecret = process.env.GHSECRET;
const accessToken = await getGHAccessToken(code, ghid, ghsecret);
const ghdata = await getGHUser(accessToken);
//console.log(ghdata);
if(ghdata) {
req.session.username = ghdata.login;
//console.log(req.session.username);
res.redirect("/");
} else {
console.log('Login failed');
res.redirect("/login.html");
}
});
app.get("/chatroom/username", bodyparser.json(), (req, res) => {
const username = req.session.username;
res.json(username);
})
app.use(express.static("public", { extensions: "html" }));
app.get("/", (req, res) => {
//console.log(req.session.username);
if(req.session.username) {
res.sendFile(__dirname + "/public/nav.html");
} else {
res.redirect("/login.html");
}
});
app.get("/nav.html", (req, res) => {
if(req.session.username) {
res.sendFile(__dirname + "/public/nav.html");
} else {
res.redirect("/login.html");
}
});
app.get("/chatroom/:roomID", (req, res) => {
if(req.session.username) {
res.sendFile(__dirname + "/public/chatroom.html");
} else {
res.redirect("/login.html");
}
});
app.get("/chatroom/js/paper-full.js", (req, res) => {
res.sendFile(__dirname + "/public/js/paper-full.js");
});
app.get("/chatroom/js/logout.js", (req, res) => {
res.sendFile(__dirname + "/public/js/logout.js");
});
app.get("/chatroom/js/script.js", (req, res) => {
res.sendFile(__dirname + "/public/js/script.js");
});
app.get("/chatroom/css/style.css", (req, res) => {
res.sendFile(__dirname + "/public/css/style.css");
});
app.get("/chatroom/js/draw.js", (req, res) => {
res.sendFile(__dirname + "/public/js/draw.js");
});
app.post("/logout", (req, res) => {
req.session = null;
res.clearCookie();
res.redirect('/login.html');
});
const port = process.env.PORT || 3000;
server.listen(port, () => console.log(`Server started on port ${port}`));