-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
239 lines (218 loc) · 5.98 KB
/
app.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
const express = require("express");
const path = require("path");
const morgan = require("morgan");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const passport = require("passport");
const Strategy = require("passport-google-oauth20").Strategy;
const TimeRecord = require("./models/time-record").TimeRecord;
const session = require("express-session");
const ensureAuth = require("connect-ensure-login");
const uuid = require("uuid/v4");
const helmet = require("helmet");
const fs = require("fs");
const repo = require("./repositories/repository")(
process.env.SERVICE_ACCOUNT_KEY,
process.env.SPREADSHEET_ID
);
const cacheService = require("./services/cache")(repo, new Map());
const timerecordService = require("./services/timerecords")(repo, cacheService);
// include and initialize the rollbar library with your access token
var Rollbar = require('rollbar');
var rollbar = new Rollbar(process.env.ROLLBAR_ACCESS_TOKEN);
configureAuth();
const app = configureApp();
// Define routes.
app.get("/", function (req, res, next) {
res.redirect("/hours/auth/google");
});
app.get(
"/auth/google",
passport.authenticate("google", {
scope: "https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email"
})
);
app.get(
"/auth/google/callback",
passport.authenticate("google", {
failureMessage: "leider konntest du nicht authentifiziert werden"
}),
function (req, res) {
// Successful authentication, redirect home.
res.redirect("/hours/timerecords");
}
);
app.get(
"/timerecords",
ensureAuth.ensureLoggedIn("/hours/auth/google"),
function (req, res, next) {
const email = req.user.emails[0].value;
Promise.all([
timerecordService.getFormViewModel(email),
timerecordService.getUserRecords(email)
])
.then(values => {
const model = {
formModel: values[0],
timeRecords: values[1]
};
res.render("index", model);
})
.catch(e => next(e));
}
);
app.get(
"/alltimerecords/:email?",
ensureAuth.ensureLoggedIn("/hours/auth/google"),
function (req, res, next) {
let email = req.params.email;
if (!email) {
email = req.user.emails[0].value;
}
Promise.all([
timerecordService.getAuthorizedUsers(),
timerecordService.getCurrentYearUserRecords(email)
])
.then(values => {
const model = {
authorisedUsers: values[0],
timerecords: values[1]
};
res.render("timerecords", model);
})
.catch(e => next(e));
}
);
app.post(
"/timerecords/:id/delete",
ensureAuth.ensureLoggedIn("/hours/auth/google"),
function (req, res, next) {
const id = req.body.id;
repo
.deleteRowById(req.user.emails[0].value, id)
.then(() => res.redirect("/hours/timerecords"))
.catch(e => next(e));
}
);
app.post(
"/timerecords/add",
ensureAuth.ensureLoggedIn("/hours/auth/google"),
function (req, res, next) {
const id = uuid().toString();
const email = req.user.emails[0].value;
const username = req.user.displayName;
const duration = req.body.timerecord.duration;
const category = req.body.timerecord.category;
const workinggroup = req.body.timerecord.workinggroup;
const description = req.body.timerecord.description;
const year = req.body.timerecord.year;
const month = req.body.timerecord.month;
const day = req.body.timerecord.day;
const newRecord = new TimeRecord(
id,
email,
username,
duration,
category,
workinggroup,
description,
year,
month,
day
);
repo
.addNewTimeRecord(newRecord)
.then(() => res.redirect("/hours/timerecords"))
.catch(e => next(e));
}
);
app.get("/healthz", (req, res, next) => {
res.status(200).end();
// timerecordService
// .getUserRecords("test@test.com")
// .then(model => {
// res.status(200).end();
// })
// .catch(e => res.status(500).end());
});
app.use(rollbar.errorHandler());
function configureAuth() {
passport.use(
new Strategy(
{
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: process.env.CALLBACK_URL
},
function (accessToken, refreshToken, profile, cb) {
repo.getAuthorizedUsers().then(authenticatedUsers => {
const foundEmail = authenticatedUsers.find(
e => {
for (let index = 0; index < profile.emails.length; index++) {
const em = profile.emails[index];
if (em && em.value === e.email) {
return true
}
}
return false;
}
);
if (foundEmail) {
return cb(null, profile);
} else {
return cb(new Error("User " + profile.emails[0].value + " is not authorized!"), null);
}
});
}
)
);
passport.serializeUser(function (user, cb) {
cb(null, user);
});
passport.deserializeUser(function (obj, cb) {
cb(null, obj);
});
}
function configureApp() {
// Create a new Express application.
var app = express();
app.set("views", path.join(__dirname, "/views"));
app.set("view engine", "hbs");
const accessLogStream = fs.createWriteStream(
path.join(__dirname, "./logs/access.log"),
{
flags: "a"
}
);
app.use(
helmet({
frameguard: false
})
);
app.use(
morgan("combined", {
stream: accessLogStream
})
);
app.use(cookieParser());
app.use(
bodyParser.urlencoded({
extended: true
})
);
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {}
})
);
// Initialize Passport and restore authentication state, if any, from the
// session.
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, "public")));
return app;
}
module.exports = app;