forked from JianShan-1214/MCU-up
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
109 lines (94 loc) · 2.88 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
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const pug = require("pug");
const mongoose = require("mongoose");
const helmet = require('helmet');
/* Authentication */
const passport = require("passport");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const session = require("express-session");
const MongoStore = require('connect-mongo');
const flash = require('connect-flash');
/* Global Middlewares */
const preRenderMiddleware = require("./services/preRenderMiddleware");
/* Routers */
const courseRouter = require("./routes/course");
const authRouter = require("./routes/auth");
const exampleRouter = require("./routes/example");
const port = process.env.PORT || 3000;
const app = express();
app.enable("trust proxy"); // ensure the proxy is enabled
app.use(helmet({
contentSecurityPolicy: false/*{
reportOnly: true
}*/
}));
/*Auth Initializaion */
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
store: MongoStore.create({ mongoUrl: process.env.DATABASE_LINK })
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
const User = require("./models/schema/user_model");
passport.use(User.createStrategy());
passport.serializeUser(function (user, done) {
done(null, user.googleID);
});
passport.deserializeUser(function (user, done) {
User.findOne({googleID: user}, function (err, user) {
done(err, user);
});
});
passport.use(
new GoogleStrategy(
{
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "/auth/google/callback",
//userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo",
},
function (accessToken, refreshToken, profile, cb) {
if ( (profile._json.hd === "me.mcu.edu.tw") /*&& (!(process.env.WHITELIST_MODE === 'true') || inWhitelist(profile._json.email))*/){
User.findOrCreate({ username: profile.emails[0].value, googleID: profile.id}, (err, user)=>{
return cb(err, user);
});
}
else{
return cb(null, false ,{message: "Invalid domain name."});
}
}
)
);
/* Database Connection Initializaion*/
mongoose.connect(process.env.DATABASE_LINK, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
app.use(express.static("public"));
app.set("view engine", "pug");
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.use(preRenderMiddleware);
app.get("/", (req, res) => {
res.render("home", {isAuthenticated:req.isAuthenticated()});
});
app.use('/test',exampleRouter);
app.use('/auth',authRouter);
app.use('/courses',courseRouter);
app.use(function(req, res, next) {
res.status(404).render('error');
});
app.listen(port, () => {
console.log("using database: " + process.env.DATABASE_LINK);
console.log("server starting on port: " + port);
});