/
login.js
108 lines (98 loc) · 3.81 KB
/
login.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
/** Copyright 2015 Board of Trustees of University of Illinois
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const router = express.Router();
const fs = require('fs');
const passport = require('passport');
var argv = require('minimist')(process.argv.slice(2));
var env = argv["e"] || 'production';
const db = require('../db/db');
// Get the mustache page that will be rendered for the login route
//const loginMustache = fs.readFileSync(mustachePath + 'login.mustache').toString();
// Render the login mustache page; if account is authenticated, just bring user to dashboard
router.get('/login', function (request, response) {
var redirectPath;
if (typeof request.query.redirectPath != "undefined") {
redirectPath = request.query.redirectPath;
} else {
redirectPath = '/courses';
}
if (request.isAuthenticated()) {
response.redirect('../courses');
} else {
if (env === "dev") {
response.redirect('/devlogin?redirectPath=' + encodeURIComponent(redirectPath));
} else {
response.redirect('/auth/google?redirectPath=' + encodeURIComponent(redirectPath));
}
}
});
// Use Passport to authentication the login information
router.get('/devlogin', function (request, response, next) {
request.body.username = 'testuser@illinois.edu';
request.body.password = "Test123!";
passport.authenticate('local', function (err, user, info) {
// Display error if failed to login; otherwise, redirect to dashboard
if (!user) {
response.send({ message: info.message, html: '../login' });
} else {
request.logIn(user, async function (err) {
await logLogin(request, "devlogin");
if (typeof request.query.redirectPath != "undefined") {
response.redirect(request.query.redirectPath);
} else {
response.redirect('../courses');
}
});
}
})(request, response, next);
});
// Use Passport to authentication the login information
router.post('/login/submit', function(request, response, next) {
passport.authenticate('local', function(err, user, info) {
// Display error if failed to login; otherwise, redirect to dashboard
if (!user) {
response.send({ message: info.message, html: '../login'});
} else {
request.logIn(user, function(err) {
response.send({ message: 'success', html: '../courses' });
});
}
})(request, response, next);
});
router.get('/auth/google',
function (req, res, next) {
passport.authenticate('google', {
scope: ['profile', 'email'],
state: req.query.redirectPath
})(req, res, next);
});
router.get('/auth/google/callback', function (req, res, next) {
passport.authenticate('google', function (err, user, info) {
if (err) { return next(err) }
if (!user) {
return res.send({ message: info.message, html: '/' })
}
req.logIn(user, async function (err) {
if (err) { return next(err); }
await logLogin(req, "googleauth");
if (typeof req.query.state != "undefined" && req.query.state.length > 0) {
return res.redirect(req.query.state);
}
return res.redirect('/courses');
});
})(req, res, next);
});
async function logLogin(req, item) {
userId = req.user.id;
mailId = req.user.mailId;
json = {};
json.mailId = mailId;
json.time = new Date().toISOString();
console.log(json);
await db.addLogs(userId, "", "login", item, json.time, JSON.stringify(json));
}
module.exports = router;