HTML and CSS Reference
In-Depth Information
req.session.user_id = user.id;
req.session.user_name = user.name;
res.redirect('/game');
});
} else {
req.facebook.app(function(app) {
res.render('login.ejs', {
layout: false,
req: req,
app: app
});
});
}
}
app.get('/',login_page);
app.post('/',login_page);
function authenticated(method) {
return function(req,res) {
if (req.session.user_id) {
method(req,res);
} else {
res.redirect('/');
}
}
}
app.get('/game',authenticated(function(req,res) {
res.end("You are: " + req.session.user_name );
}));
The server has three main parts. The first part sets up defaults along with app as an express-powered server.
The second part deals with the home page, which allows Facebook login. Lastly, the authenticated meth-
od and the /game page is used by the game to verify you are logged in to actually play the game.
The top of the file needs to be modified with your Facebook App ID, your Facebook App Secret, and a ran-
dom string used to encode the session. You should change the random string to prevent users from modifying
their session data, but you can also leave it as is until you deploy. The boolean OR || is used to check environ-
ment variables for a corresponding value first. Heroku uses environment variables to store configuration data to
make it easier to use different values for development and production.
The express app is created. It sets up the various pieces of the server needed, including logging, a public
directory for serving files, the body and cookie parsers, a session for tracking who is logged in, and the face-
plate middleware used for Facebook authentication using OAuth2. The app is told to listen on the specified
port, which either defaults to 3000 or the port set up by the hosting server.
The login_page method is used as the homepage of the application. Its job is to check if the user has a
valid session token from Facebook and if so save the user's Facebook ID and name in the session and redirect
Search WWH ::




Custom Search