-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
123 lines (111 loc) · 4.13 KB
/
app.js
File metadata and controls
123 lines (111 loc) · 4.13 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
// Program entry point
// Load libraries
var express = require('express'),
app = express(),
async = require('async'),
mongoose = require('mongoose'),
fs = require('fs'),
passport = require('passport'),
ghStrat = require('passport-github').Strategy,
engine = require('ejs-locals');
// Bootstrap models
var models_path = __dirname + '/app/models',
model_files = fs.readdirSync(models_path);
model_files.forEach(function (file) {
require(models_path+'/'+file);
});
var user_ctrl = require('./app/controllers/user_controller');
// Load the github client ID and client secret values
// NOTE: These must exist for the app to work.
// They are not in source so ensure you create them or get them from someone!
var GITHUB_CLIENT_ID = process.env.CLIENT_ID,
GITHUB_CLIENT_SECRET = process.env.CLIENT_SECRET;
if (!GITHUB_CLIENT_ID || !GITHUB_CLIENT_SECRET) {
console.log('ERROR! GitHub auth file missing: cannot continue. '+
'Supply a github auth file that has client id and secret id to allow user login. '+
'\n\nIf you ask super nice, ngallagher87 will give them to you.');
process.exit(1);
}
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
user_ctrl.findID(id, function(err, user) {
done(err, user);
})
});
// Use the ghStrat within Passport.
// Strategies in Passport require a `verify` function, which accept
// credentials (in this case, an accessToken, refreshToken, and GitHub
// profile), and invoke a callback with a user object.
passport.use(new ghStrat({
clientID: GITHUB_CLIENT_ID,
clientSecret: GITHUB_CLIENT_SECRET,
callbackURL: "/auth/github/callback"
},
function(accessToken, refreshToken, profile, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
User = mongoose.model('User');
// Here we recieved the GitHub profile of the user.
// Now we need to check if there is a user record tied to this email address
// If there is, return the user profile
// If not, create a user profile and return that
User.findOne({ email: profile.emails[0].value }, function (err, foundUser) {
if (err || foundUser === null) {
console.log('creating new user/fireteam');
console.log(profile);
// Generate a display name
var displayName = profile.displayName;
if (typeof displayName === 'undefined')
displayName = profile.name + '\'s';
// This user doesn't exist, so create them
newUser = new User({
email: profile.emails[0].value,
name: profile.name,
displayName: displayName,
profile: profile
});
// Create a fireteam for the new user
Fireteam = mongoose.model('Fireteam');
var newFire = new Fireteam({
name: newUser.displayName + '\'s Battalion',
stats: {
wins: 0,
losses: 0
}
});
newFire.generateDefaultTeam(function(err, soldiers){
// Wait til we generate the team until we save the user
console.log(soldiers);
newUser.fireteam = newFire._id;
newFire.soldiers = soldiers;
newFire.save();
newUser.save();
return done(null, newUser);
});
} else {
console.log('return found user');
// This user exists already so return them
return done(null, foundUser);
}
});
});
}
));
// Configure express
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.engine('.html', engine);
app.use(express.logger());
app.use(express.cookieParser('nuclear goat'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session());
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(__dirname));
var server = require("./server");
server.start(app);