-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.remote.js
123 lines (105 loc) · 3.98 KB
/
app.remote.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
/**
* Module dependencies.
*/
var express = require('express'),
http = require('http'),
routes = require('./routes'),
passport = require('passport'),
BrowserIdStrategy = require('passport-browserid').Strategy;
// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session. Typically,
// this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing. However, since this example does not
// have a database of user records, the BrowserID verified email address
// is serialized and deserialized.
passport.serializeUser(function(user, done) {
done(null, user.email);
});
passport.deserializeUser(function(email, done) {
done(null, { email: email });
});
// Use the BrowserIDStrategy within Passport.
// Strategies in passport require a `validate` function, which accept
// credentials (in this case, a BrowserID verified email address), and invoke
// a callback with a user object.
passport.use(new BrowserIdStrategy({
audience: 'http://battleship.gotdibbs.net' // CHANGE THIS PER SERVER
},
function(email, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
// To keep the example simple, the user's email address is returned to
// represent the logged-in user. In a typical application, you would want
// to associate the email address with a user record in your database, and
// return that user instead.
return done(null, { email: email })
});
}
));
// Simple route middleware to ensure user is authenticated.
// Use this route middleware on any resource that needs to be protected. If
// the request is authenticated (typically via a persistent login session),
// the request will proceed. Otherwise, the user will be redirected to the
// login page.
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login')
}
/*-----
----- BEGIN NODE.JS SERVER SETUP -----
-----*/
var app = express();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({ secret: 'apple-p1e' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', routes.getIndex);
app.get('/fast', ensureAuthenticated, routes.getFast);
app.get('/visual', routes.getVisual);
app.get('/upload', ensureAuthenticated, routes.getUpload);
app.post('/upload', ensureAuthenticated, routes.postUpload);
app.get('/api/ais', routes.api.getAIs);
app.get('/api/games/:player1/:player2/:gameCount', routes.api.getGames);
app.get('/login', function(req, res){
res.render('login', { user: req.user, title: 'Battleship: Login', id: 'login' });
});
// POST /auth/browserid
// Use passport.authenticate() as route middleware to authenticate the
// request. BrowserID authentication will verify the assertion obtained from
// the browser via the JavaScript API.
app.post('/auth/browserid',
passport.authenticate('browserid', { failureRedirect: '/login' }),
function(req, res) {
if (req.user && /@sonomapartners.com$/.test(req.user.email)) {
res.redirect('/');
}
else {
req.logout();
res.redirect('/login');
}
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
http.createServer(app).listen(process.env.PORT, function(){
console.log('Express server listening on port %d in %s mode', process.env.PORT, app.settings.env);
}).on('connection', function (socket) {
socket.setTimeout(5 * 60 * 1000); // 5 minute timeout
});