-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·94 lines (77 loc) · 2.47 KB
/
server.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
// Import dependencies.
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var favicon = require('serve-favicon');
var passport = require('passport');
var util = require('util');
var session = require('express-session');
var sequelize = require('sequelize');
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var partials = require('express-partials');
// Import models.
var db = require('./models');
// Initialize Express app.
var app = express();
// Session middleware.
app.use(partials());
var sessionSecret;
try {
sessionSecret = require('./config/sessionSecret.js');
} catch(e) {
sessionSecret = process.env.sessionSecret;
}
app.use(session({ secret: sessionSecret, resave: false, saveUninitialized: false }));
// Initialize Passport.
app.use(passport.initialize());
app.use(passport.session());
// Serve static content for the app from the 'public' directory in the application directory.
app.use(express.static(path.join(__dirname, 'public')));
// Use Heroku's assigned PORT or 3000.
var PORT = process.env.PORT || 3000;
// Serve favicon.
app.use(favicon(path.join(__dirname, 'public/assets/images', 'favicon.png')));
// Body Parser middleware.
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// Import routes and give the server access to them.
var passportRoutes = require('./controllers/passport_controller');
var mainRoutes = require('./controllers/main_controller');
app.use('/', passportRoutes);
app.use('/', mainRoutes);
// Use local or Heroku copies of Passport secrets.
var authConfig;
try {
authConfig = require('./config/auth');
} catch(err) {
authConfig =
{
google: {
clientID: process.env.clientID,
clientSecret: process.env.clientSecret,
callbackURL: process.env.callbackURL
}
};
}
// Passport methods.
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (obj, done) {
done(null, obj);
});
// Use the GoogleStrategy within Passport.
passport.use(new GoogleStrategy(
authConfig.google,
function(accessToken, refreshToken, profile, done) {
process.nextTick(function () {
return done(null, profile);
});
}
));
// Sync with Sequelize and start listening.
db.sequelize.sync().then(function() {
app.listen(PORT, function () {
console.log('listening on port ' + PORT);
});
});