-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
167 lines (122 loc) · 4.32 KB
/
main.js
File metadata and controls
167 lines (122 loc) · 4.32 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const url = require('url');
const session = require('express-session');
var FileStore = require('session-file-store')(session);
const app = express();
const { google } = require('googleapis');
const axios = require('axios');
const tokenService = require('./services/tokenService');
require('custom-env').env();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
secret: 'secret-key',
resave: false,
store: new FileStore,
saveUninitialized: false,
}));
app.use(require('./routes'));
app.use(express.static(path.join(__dirname, './sol/build')));
// Example protected and unprotected routes
// app.get('/', (req, res) => res.redirect("http://localhost:3000"));
//app.get('/login', (req, res) => res.send('Please log in'));
app.get('/auth/google/callback', async (req, res) => {
//Make the oAuth client
const oAuth2Client = new google.auth.OAuth2(process.env.CLIENT_ID, process.env.CLIENT_SECRET, process.env.CALLBACK_URL);
//Get the code to get the token from the url
const queryObject = url.parse(req.url, true);
const code = queryObject.query.code;
//Get the token using the code
var tokenRes = await oAuth2Client.getToken(code);
var token = tokenRes.tokens;
//Set the oAuthClient to use the token that we just got
oAuth2Client.setCredentials(token);
//Make an oauth2 client using the oAuthClient we just made
var oauth2 = google.oauth2({
auth: oAuth2Client,
version: 'v2'
});
//Get the user's info
var user = await oauth2.userinfo.get();
//Make a variable called userData
var userToken;
//If the user's data is not stored in the session
if (!req.session.user) {
req.session.user = {};
//Store the user's data into the session
req.session.user.primary = user.data;
req.session.user.google = [];
req.session.user.canvas = [];
//Read file that has user data in it
userToken = await tokenService.getToken(req.session.user.primary.id);
if (!userToken) {
userToken = { primary: {}, google: [], canvas: [] };
userToken.primary = token;
userToken._id = req.session.user.primary.id;
await tokenService.insertToken(userToken);
}
if (userToken.google.length >= 1) {
for (secondaryToken of userToken.google) {
//Set the oAuthClient to use the token that we just got
oAuth2Client.setCredentials(secondaryToken);
//Make an oauth2 client using the oAuthClient we just made
oauth2 = google.oauth2({
auth: oAuth2Client,
version: 'v2'
});
var secondaryUser = await oauth2.userinfo.get();
req.session.user.google.push(secondaryUser.data);
}
}
if (userToken.canvas.length >= 1) {
for (canvasToken of userToken.canvas) {
const account = await axios.get(`https://rchs.instructure.com/api/v1/users/self`, {
params: {
access_token: canvasToken,
}
});
req.session.user.canvas.push(account.data);
}
}
res.redirect("/assignments");
}
else {
req.session.user.google.push(user.data);
userToken = await tokenService.getToken(req.session.user.primary.id);
userToken.google.push(token);
await tokenService.updateToken(req.session.user.primary.id, userToken);
res.redirect('/profile');
}
});
app.get('/api/logout', (req, res) => {
req.session.destroy(function (err) {
if (err) {
console.log("--> session destroy failed.err -> ", err);
}
});
res.redirect('/');
});
app.get('*', (req, res) => {
console.log(path.join(__dirname, '/sol/build', 'index.html'));
res.sendFile(path.join(__dirname, '/sol/build', 'index.html'));
});
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
});
module.exports = app;