forked from osu-cs419-w20/canvas-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
150 lines (128 loc) · 4.79 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
const express = require('express');
const request = require('request');
const cookieParser = require('cookie-parser');
const app = express();
const port = process.env.PORT || 5000;
const TOKEN = 'token';
const userToken = ''
app.use(cookieParser());
app.use(express.json());
// store login in redux, and reset this on every call to server, if fails
// redirect to login.
app.listen(port, () => console.log(`Listening on port ${port}`));
app.get('/loggedIn', (req, res) => {
console.log(req.cookies[TOKEN]);
if ((TOKEN in req.cookies) && req.cookies[TOKEN] != null) {
res.status(200).send(true);
} else {
res.send(false);
}
});
// set the token given by the user
app.post('/login', (req, res) => {
// secure would be set to secure in a production version of this to protect
// the token but because secure requres https we cant use it with localhost
// path is set to '/' by default which means this can be used by any path in
// the website
// httpOnly limits the cookie to being accessed only on the server
// expires in 3 days represented by milliseconds * seconds * minutes * hours * days
res.cookie(TOKEN, req.body.token,
{ expires: new Date(Date.now() + (1000 * 60 * 60 * 24 * 3)), httpOnly: true, sameSite: "none"});
res.send(true);
});
app.get('/logout', (req, res) => {
console.log("cookies before: ", req.cookies);
res.clearCookie(TOKEN, { httpOnly: true, sameSite: "none"});
console.log("cookies after: ", req.cookies);
res.send(false);
});
// handle any querys that are part of the api call
function stringifyQuery(queryArray) {
let query = "";
// check if query paramerters actually exist
if (Object.keys(queryArray).length !== 0) {
query = "?";
// sort through each one and convert it to string
Object.keys(queryArray).map(key => {
// array querys need to be handled a little diffrent
if (Array.isArray(queryArray[key])) {
query += queryArray[key].map(elem => `${key}[]=${elem}&`);
// this is to get rid of a random , that keeps showing up
// no idea why
query = query.replace(',', '');
} else {
query += `${key}=${queryArray[key]}&`
}
});
query = query.replace(',', '');
// get rid of the trailing & that the above code adds
query = query.slice(0, -1);
}
return query;
}
// make get requests to the canvas api
app.get('/get/*', (req, res) => {
// handle any querys that are part of the api call
let query = stringifyQuery(req.query);
console.log(`\nRequest made for: https://canvas.instructure.com/api/v1/${req.params[0]}${query}`);
request({
url: `https://canvas.instructure.com/api/v1/${req.params[0]}${query}`,
headers: {
'Authorization': `Bearer ${req.cookies[TOKEN]}`,
}
},
(error, response, body) => {
if (!error && response.statusCode == 200) {
res.status(200).send({ results: body});
console.log("Success: ", response.statusCode);
} else {
res.status(response.statusCode);
console.log("Error: ", response.status);
}
}
);
});
app.post('/post/*', (req, res) => {
console.log(`\nRequest made for: https://canvas.instructure.com/api/v1/${req.params[0]}`);
console.log("body: ", req.body);
request({
url: `https://canvas.instructure.com/api/v1/${req.params[0]}`,
method: 'POST',
headers: {
'Authorization': `Bearer ${req.cookies[TOKEN]}`,
},
body: JSON.stringify(req.body)
},
(error, response, body) => {
if(!error && response.statusCode == 200) {
console.log("Success: ", response.statusCode);
res.status(response.statusCode).end();
} else {
console.log("Error: ", response.statusCode);
res.status(response.statusCode).end();
}
});
});
// #7986CB
app.post('/put/*', (req, res) => {
console.log(`\nRequest made for: https://canvas.instructure.com/api/v1/${req.params[0]}`);
console.log("body: ", req.body);
request({
url: `https://canvas.instructure.com/api/v1/${req.params[0]}`,
method: 'PUT',
headers: {
'Authorization': `Bearer ${req.cookies[TOKEN]}`,
},
body: JSON.stringify(req.body)
},
(error, response, body) => {
if(!error && response.statusCode == 200) {
console.log("Success: ", response.statusCode);
res.status(response.statusCode).end();
} else {
console.log("Error: ", response.statusCode);
console.log("Body: ", body);
res.status(response.statusCode).end();
}
});
});