-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
126 lines (105 loc) · 3.41 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
const createError = require('http-errors');
const cookieParser = require('cookie-parser');
const compression = require('compression');
const express = require('express');
const flash = require('connect-flash');
const logger = require('morgan');
const path = require('path');
const sslRedirect = require('heroku-ssl-redirect');
const app = express();
app.locals.env = process.env;
app.use(compression());
// Mongoose setup
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URI, {
useCreateIndex: true,
useNewUrlParser: true
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'Mongoose connection error:'));
db.once('open', function () {
console.log('Mongoose connected.')
});
// Redis setup
const redis = require('redis');
const redisClient = redis.createClient({url: process.env.REDIS_URL});
// Epic Games setup
const epicGamesController = require('./controllers/epic-games-controller');
// Passsport setup
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const session = require('express-session');
const RedisStore = require('connect-redis')(session);
app.use(session({
store: new RedisStore({
client: redisClient,
logErrors: true
}),
secret: '{#fFT"5pZ>LvD#N:',
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
const User = require('./models/user');
passport.use(new LocalStrategy(function (username, password, done) {
User.findOne({email: username}, async function (err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false);
}
if (!user.verifyPassword(password)) {
return done(null, false);
}
// Update user's Epic Games account information if it exists
if (user.epicGamesAccount) {
let profile = await epicGamesController.getProfile(user.epicGamesAccount.id);
user.epicGamesAccount.displayName = profile.displayName;
await user.save();
}
return done(null, user);
});
}));
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (user, done) {
done(null, user);
});
// View engine setup
const apiRouter = require('./routes/api');
const authRouter = require('./routes/auth');
const indexRouter = require('./routes/index');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
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')));
// Enable SSL redirect
app.use(sslRedirect());
app.use('/', indexRouter);
app.use('/api', apiRouter);
app.use('/auth', authRouter);
if (process.env.NODE_ENV !== 'production') {
const testingRouter = require('./routes/testing');
app.use('/testing', testingRouter);
}
// 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);
res.render('error');
});
module.exports = app;