Created
July 5, 2013 15:33
-
-
Save hrajchert/5935329 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'), | |
app = express(), | |
// ... | |
passport = require('passport'), | |
MongoStore = require('connect-mongo')(express), | |
Flash = require('connect-flash'); | |
// View stuff | |
app.configure(function(){ | |
// Indicate port to use | |
app.set('port', process.env.PORT || 3000); | |
// Setup static files | |
app.use(express.static(path.join(__dirname, 'public'))); | |
// ... | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(express.cookieParser()); | |
app.use(express.session({store: new MongoStore({db:'express-sessions'}), secret: 'SOMETHINGSECRET-HERE' })); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
app.use(Flash()); | |
app.use(app.router); | |
app.use(not_found); | |
}); | |
// more stuff | |
// Configure rest end points | |
require('./rest/')(app); | |
// Configure the routes (the actual pages) | |
require('./routes')(app); | |
// Configure auth (facebook login and all that) | |
require('./auth').configureExpress(app); | |
// Create the server | |
server.listen(app.get('port'), function(){ | |
console.log("Express server listening on port " + app.get('port')); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
passport = require 'passport' | |
FacebookStrategy = require('passport-facebook').Strategy | |
createUserFromFb = (fbProfile,access_token) -> | |
ans = | |
first_name: fbProfile['name']['givenName'] | |
last_name: fbProfile['name']['familyName'] | |
fb_id: fbProfile['id'] | |
gender: fbProfile['gender'] | |
fb_access_token: access_token | |
isAdmin : false | |
facebookConf = require './settings/facebook.json' | |
facebookStrategyConf = | |
clientID: facebookConf.app_id, | |
clientSecret: facebookConf.app_secret, | |
callbackURL: "YOURPUBLICURL/auth/facebook/callback" | |
# Add facebook auth | |
passport.use new FacebookStrategy facebookStrategyConf, (accessToken, refreshToken, profile, done) -> | |
ensureCollection = require('./dal').ensureCollection | |
# Get the users collection | |
ensureCollection {db:'news', collection:'users'}, (err, collection) -> | |
collection.findOne {fb_id: profile['id']}, (err,user) -> | |
if err | |
throw err | |
if not user? | |
console.log 'Creating user!' | |
user = createUserFromFb profile, accessToken | |
# Insert the new user | |
collection.insert user, {safe:true}, (error,user) -> | |
console.log 'user inserted' | |
else | |
console.log 'recurring user' | |
done null, user | |
passport.serializeUser = (user, done) -> | |
done null, user | |
passport.deserializeUser = (obj, done) -> | |
done null, obj | |
exports.configureExpress = (app) -> | |
app.get '/auth/facebook', passport.authenticate 'facebook' | |
app.get '/auth/facebook/callback', passport.authenticate 'facebook', { successRedirect: '/', failureRedirect: '/login' } | |
app.get '/logout', (req, res) -> | |
req.session.destroy(); | |
res.redirect('/'); | |
exports.needsAdmin = (req,res,next) -> | |
# Add the user from the session | |
if req.user? and req.user.isAdmin | |
next() | |
else | |
req.flash "error", "You need to be admin to access here" | |
res.redirect '/login' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Gist dont allow me, but this is under routes/admin directory | |
module.exports = (app,base_url) -> | |
# Make sure users from here on are admin users (base_url forward) | |
app.all base_url + '/*', app.info.auth.needsAdmin | |
# Include the user admin pages | |
require('./user')(app,base_url) | |
# Include other admin modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Gist dont allow me, but this is under routes directory | |
# configure all the pages | |
module.exports = (app) -> | |
# Add general information to all requests | |
app.all '*', (req,res,next) -> | |
# Add the user from the session | |
if req.user? | |
res.locals.session_user = req.user | |
# Add the flash messages | |
res.locals.flash_messages = req.flash() | |
next() | |
# Include the admin plugin with the base url /admin | |
require('./admin')(app,'/admin') | |
# Include the rest of the pages |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment