-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminiServer4.js
More file actions
479 lines (416 loc) · 16.4 KB
/
Copy pathminiServer4.js
File metadata and controls
479 lines (416 loc) · 16.4 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
"use strict";
/* this is miniserver3.js*/
const express = require('express')
const port = 51543// you need to put your port number here //52193
const APIrequest = require('request');
const http = require('http');
const sqlite3 = require("sqlite3").verbose(); // use sqlite
const fs = require("fs"); // file system
const dbFileName = "Flashcards.db";
// makes the object that represents the database in our code
const db = new sqlite3.Database(dbFileName); // object, not database.
const APIkey = ""; // ADD API KEY HERE
const url = "https://translation.googleapis.com/language/translate/v2?key="+APIkey
const passport = require('passport'); //use passport.js in node
const cookieSession = require('cookie-session'); // use cookie-session in node
const GoogleStrategy = require('passport-google-oauth20'); // use passport-oauth20 in node
const sqlite = require('sqlite3'); // use SQLlite in node
const googleLoginData = {
clientID: '',
clientSecret: '',
callbackURL: '/auth/redirect' // redirect the access code to /auth/redirect in server
};
function queryHandler(req, res, next) {
let reqUrl = req.url;
let qObj = req.query;
/* this is testAPI.js*/
// An object containing the data expressing the query to the
// translate API.
// Below, gets stringified and put into the body of an HTTP PUT request.
let requestObject =
{
"source": "en",
"target": "zh-CN",
"q": [
qObj.english
]
}
console.log("English phrase: ", requestObject.q[0]);
// The call that makes a request to the API
// Uses the Node request module, which packs up and sends off
// an HTTP message containing the request to the API server
APIrequest(
{ // HTTP header stuff
url: url,
method: "POST",
headers: {"content-type": "application/json"},
// will turn the given object into JSON
json: requestObject },
// callback function for API request
APIcallback
);
// callback function, called when data is received from API
function APIcallback(err, APIresHead, APIresBody) {
// gets three objects as input
if ((err) || (APIresHead.statusCode != 200)) {
// API is not working
console.log("Got API error");
console.log(APIresBody);
} else {
if (APIresHead.error) {
// API worked but is not giving you data
console.log(APIresHead.error);
} else {
// console.log("In Chinese: ",
// APIresBody.data.translations[0].translatedText);
// console.log("\n\nJSON was:");
// console.log(JSON.stringify(APIresBody, undefined, 2));
// print it out as a string, nicely formatted
console.log(qObj);
if (qObj.english != undefined) {
res.json( {"English" : qObj.english, "Chinese": APIresBody.data.translations[0].translatedText} );
}
else {
next();
}
}
}
} // end callback function
/*this is the end of testAPI.js*/
}
function queryHandler2(req, res, next) {
let reqUrl = req.url;
let qObj = req.query;
//qObj.english
//qObj.chinese
console.log(qObj);
if ((qObj.english != undefined) && (qObj.chinese != undefined)){
const cmdStr = 'INSERT into Flashcards (user, english, chinese, seen, correct, score) VALUES (@0, @1, @2, 0, 0, 0)';
let userData1 = JSON.parse(req.user.userData);
console.log("Insert into Flashcards table:",userData1.GoogleID ,qObj.english, qObj.chinese);
db.run(cmdStr, userData1.GoogleID,qObj.english, qObj.chinese, insertCallBack);
function insertCallBack(err){
if (err) { res.json({response: err});}
else { res.json({"English" : qObj.english, "Chinese": qObj.chinese});}
}
}
else {
next();
}
// function insertCallBack(err){
// if (err) { res.json({response: err});}
// else { res.json({"English" : qObj.english, "Chinese": qObj.chinese});}
// }
}
function nameQueryHandler(req, res, next) {
let url = req.url;
let qObj = req.query;
console.log(qObj);
console.log("nameQueryHandler here");
//qObj.english
//qObj.chinese
//
// console.log(qObj);
if(qObj.name!=undefined){
const cmdStr = 'SELECT firstName FROM Users WHERE GoogleID=?';
let userData1 = JSON.parse(req.user.userData);
console.log("SELECT firstName FROM Users WHERE GoogleID=?",userData1.GoogleID);
db.get(cmdStr, [userData1.GoogleID], selectCallBack);
function selectCallBack(err,data){
if (err) {
res.json({response: err});
} else if(data) {
console.log("data from name:",data);
res.json({userName:data.firstName});
} else {
next();
}
}
}
}
function getStateQueryHandler(req, res, next) {
let url = req.url;
let qObj = req.query;
console.log(qObj);
console.log("getStateQueryHandler here");
//qObj.english
//qObj.chinese
//
// console.log(qObj);
if(qObj.chinese!=undefined){
let userData1 = JSON.parse(req.user.userData);
const cmdStr = 'SELECT * FROM Flashcards WHERE user=? ORDER BY seen ASC';
console.log("SELECT chinese FROM Flashcards WHERE user=? ORDER BY seen ASC",userData1.GoogleID);
db.get(cmdStr, [userData1.GoogleID], selectChineseCallBack);
function selectChineseCallBack(err,data){
if (err) {
res.json({response: err});
} else if(data) {
console.log("data from chineseQuery:",data);
const cmdStr2 = 'UPDATE Flashcards SET seen=seen+1 WHERE chinese=?';
db.run(cmdStr2, data.chinese, updateCallBack);
function updateCallBack(err){
if (err) {
console.log(err);
} else{
console.log("seen+1;")
}
}
res.json({reviewChinese:data.chinese, reviewEnglish:data.english});
} else {
next();
}
}
}
}
function correctHandler(req, res, next) {
let url = req.url;
let qObj = req.query;
console.log(qObj);
console.log("correctHandler here");
//qObj.english
//qObj.chinese
//
// console.log(qObj);
if(qObj.chinese!=undefined){
let userData1 = JSON.parse(req.user.userData);
console.log("update score and correct!");
console.log("update score and correct!");
console.log("update score and correct!");
console.log("update score and correct!");
const cmdStr = 'UPDATE Flashcards SET correct=correct+1, score=(max(1,5-correct)+max(1,5-seen)+5*((seen-correct)/seen)) WHERE chinese=?';
db.run(cmdStr, qObj.chinese, updateCallBack);
function updateCallBack(err){
if (err) {
console.log(err);
} else{
console.log("correct+1;")
}
}
}
}
function fileNotFound(req, res) {
let url = req.url;
res.type('text/plain');
res.status(404);
res.send('Cannot find '+url);
}
passport.use( new GoogleStrategy(googleLoginData, gotProfile) );
// put together the server pipeline
const app = express();
app.use('/', printURL);
app.use(cookieSession({
maxAge: 6 * 60 * 60 * 1000, // Six hours in milliseconds
// meaningless random string used by encryption
keys: ['hanger waldo mercy dance']
}));
// Initializes request object for further handling by passport
app.use(passport.initialize());
// If there is a valid cookie, will call deserializeUser()
app.use(passport.session());
app.use(express.static('public')); // can I find a static file?
app.get('/auth/google',
passport.authenticate('google',{ scope: ['profile'] }) );
// passport.authenticate sends off the 302 response
// with fancy redirect URL containing request for profile, and
// client ID string to identify this app.
// Google redirects here after user successfully logs in
// This route has three handler functions, one run after the other.
app.get('/auth/redirect',
// for educational purposes
function (req, res, next) {
console.log("at auth/redirect");
next();
},
// This will issue Server's own HTTPS request to Google
// to access the user's profile information with the
// temporary key we got in the request.
passport.authenticate('google'),
// then it will run the "gotProfile" callback function,
// set up the cookie, call serialize, whose "done"
// will come back here to send back the response
// ...with a cookie in it for the Browser!
// function(req, res) {
// console.log("req.user", req.user);
// const cmdStr = 'if not exists(SELECT * FROM Flashcards WHERE user = ?)';
// const cmdStr2 = 'if exists(SELECT 1 FROM Flashcards WHERE user = ?)';
// db.run(cmdStr, [req.user], (err) => {
// if (err) {
// return console.error(err.message);
// }
// else {
// console.log("Flashcards are NOT found in DB table");
// res.redirect('/user/lango.html');
// }
// });
// db.get(cmdStr2, [req.user], (err,data) => {
// if (err) {
// return console.error(err.message);
// }
// else {
// console.log("Flashcards are found in DB table");
// console.log('Logged in and using cookies!');
// res.redirect('/user/review.html');
// }
// });
// if (data) {
// console.log("Flashcards are found in DB table");
// console.log('Logged in and using cookies!');
// res.redirect('/user/review.html');
// }
function(req, res){
const cmdStr = 'SELECT * FROM Flashcards WHERE user = ?' ;
db.get(cmdStr, [req.user], (err, data) => {
if (err) {
return console.error(err.message);
}
else {
if (data!==undefined) {
console.log("Flashcards are found in DB table");
console.log('Logged in and using cookies!');
res.redirect('/user/review.html');
} else {
console.log("Flashcards are found in DB table");
res.redirect('/user/lango.html');}
}
}
)
});
// static files in /user are only available after login
app.get('/user/*',
isAuthenticated, // only pass on to following function if
// user is logged in
// serving files that start with /user from here gets them from ./
express.static('.')
);
app.get('/user/footer', nameQueryHandler); //check name for footer
app.get('/user/getState', getStateQueryHandler); //getChinese for review
app.get('/user/updateCorrect', correctHandler); //getChinese for review
app.get('/user/translate', queryHandler ); // if not, is it a valid query?
app.get('/user/store', queryHandler2 ); // if not, is it a valid query?
app.use( fileNotFound ); // otherwise not found
app.listen(port, function (){console.log('Listening...');} )
function dumpDB() {
db.all ( 'SELECT * FROM flashcards', dataCallback);
function dataCallback( err, data ) {console.log(data)}
}
dumpDB();
// print the url of incoming HTTP request
function printURL (req, res, next) {
console.log(req.url);
next();
}
// function to check whether user is logged when trying to access
// personal data
function isAuthenticated(req, res, next) {
if (req.user) {
console.log("Req.session:",req.session);
console.log("Req.user:",req.user);
console.log("Req.user:",req.user.userData);
next();
} else {
res.redirect('/login.html'); // send response telling
// Browser to go to login page
}
}
// Some functions Passport calls, that we can use to specialize.
// This is where we get to write our own code, not just boilerplate.
// The callback "done" at the end of each one resumes Passport's
// internal process.
// function called during login, the second time passport.authenticate
// is called (in /auth/redirect/),
// once we actually have the profile data from Google.
// function gotProfile(accessToken, refreshToken, profile, done) {
// console.log("Google profile",profile);
//
// const cmdStr = 'SELECT * FROM Users WHERE GoogleID='+ profile.id;
// console.log("SELECT * FROM Users WHERE GoogleID=",profile.id);
// db.get(cmdStr, dataCallBack);
// let dbRowID = profile.id;
// function dataCallBack(err, data){
// if(err){
// console.log("Check if user exists failed");
// } else if(data != undefined) {
// console.log("data:", data);
// } else{
// const cmdStr = 'INSERT into Users (firstName, lastName, GoogleID) VALUES (@0, @1, @2)';
// db.run(cmdStr,profile.name.givenName, profile.name.familyName, profile.id, insertCallBack);
// console.log("insert into user", profile.name.givenName, profile.name.familyName, profile.id);
// function insertCallBack(err){
// if(err) {
// console.log("Insert profile failed");
// }
// }
// }
// }
//
// // here is a good place to check if user is in DB,
// // and to store him in DB if not already there.
// // Second arg to "done" will be passed into serializeUser,
// // should be key to get user out of database.
//
// // let dbRowID = profile.id; // temporary! Should be the real unique
// // key for db Row for this user in DB table.
// // Note: cannot be zero, has to be something that evaluates to
// // True.
//
// done(null, dbRowID);
// }
function gotProfile(accessToken, refreshToken, profile, done) {
console.log("Google profile",profile);
const cmdStr = 'SELECT * FROM Users WHERE GoogleID = ?' ;
// console.log("SELECT * FROM Users WHERE GoogleID=",profile.id);
let last6 = profile.id % 1000000;
// first row only
db.get(cmdStr, [last6], (err, row) => {
if (err) { return console.error(err.message);}
else{
if (row){ console.log(row.firstName, row.lastName, row.GoogleID);}
else {
const cmdStr2 = 'INSERT into Users (firstName, lastName, GoogleID) VALUES (@0, @1, @2)';
db.run(cmdStr2,profile.name.givenName, profile.name.familyName, last6, (err) => {if(err){console.log("Insert profile failed");}});
}
}
});
let dbRowID = last6;
// here is a good place to check if user is in DB,
// and to store him in DB if not already there.
// Second arg to "done" will be passed into serializeUser,
// should be key to get user out of database.
// let dbRowID = profile.id; // temporary! Should be the real unique
// key for db Row for this user in DB table.
// Note: cannot be zero, has to be something that evaluates to
// True.
done(null, dbRowID);
}
// Part of Server's session set-up.
// The second operand of "done" becomes the input to deserializeUser
// on every subsequent HTTP request with this session's cookie.
passport.serializeUser((dbRowID, done) => {
console.log("SerializeUser. Input is",dbRowID);
done(null, dbRowID);
});
// Called by passport.session pipeline stage on every HTTP request with
// a current session cookie.
// Where we should lookup user database info.
// Whatever we pass in the "done" callback becomes req.user
// and can be used by subsequent middleware.
passport.deserializeUser((dbRowID, done) => {
console.log("deserializeUser. Input is:", dbRowID);
// here is a good place to look up user data in database using
// dbRowID. Put whatever you want into an object. It ends up
// as the property "user" of the "req" object.
const cmdStr = 'SELECT * FROM Users WHERE GoogleID = ?' ;
db.get(cmdStr, [dbRowID], (err, row) => {
if (err) { return console.error(err.message);}
else{
if (row){
console.log("user is found in DB table Users");
let userData = {userData: JSON.stringify({firstName: row.firstName,lastName: row.lastName, GoogleID: dbRowID})};
done(null, userData);
}else {
console.log("user is not found in DB table Users");
}
}
});
});