Skip to content

Instantly share code, notes, and snippets.

@don-smith
Created September 30, 2012 06:28
Show Gist options
  • Save don-smith/3806040 to your computer and use it in GitHub Desktop.
Save don-smith/3806040 to your computer and use it in GitHub Desktop.
Facebook authentication using EveryAuth 0.2.34 (prerelease: express3 branch) with Express.js 3.0.0rc4
facebook = require './facebook'
mongoose = require 'mongoose'
express = require 'express'
routes = require './routes'
http = require 'http'
path = require 'path'
app = express()
app.configure ->
app.set 'port', process.env.PORT || 3000
app.set 'views', path.join __dirname, 'views'
app.set 'view engine', 'jade'
app.use require('stylus').middleware
src: path.join __dirname, 'public'
app.use express.static(path.join __dirname, 'public')
app.use express.cookieParser()
app.use express.session 'secret': 'rddalym'
app.use express.favicon()
app.use express.logger 'dev'
app.use express.bodyParser()
app.use express.methodOverride()
facebook.attach app
app.configure 'development', ->
mongoose.connect 'mongodb://localhost:27017/myladdr'
app.use express.errorHandler
dumpExceptions: true
showStack: true
app.configure 'production', ->
mongoUrl = 'mongodb://<username>:<password>@ds035787-a.mongolab.com:35787/myladdr'
app.use express.errorHandler()
mongoose.connect mongoUrl
routes.init app
http.createServer(app).listen app.get('port'), ->
console.log 'Express server listening on port ' + app.get 'port'
everyauth = require 'everyauth'
user = require './models/user'
fb = everyauth.facebook
# Exception handling is obviously still lacking
exports.attach = (app) ->
fb.mobile true
fb.appId '<app_id>'
fb.appSecret '<app_secret>'
fb.callbackPath '/auth/facebook/callback'
fb.entryPath '/auth/facebook'
fb.fields 'id,name,picture'
fb.findOrCreateUser (session, accessToken, accessTokenExtra, fbUserMetadata) ->
user.findFacebookUser fbUserMetadata.id, (foundUser) ->
unless foundUser
user.addFacebookUser fbUserMetadata, (newUser) ->
unless newUser
throw 'Creating the user was unsuccessful'
everyauth.facebook.user = newUser if newUser
everyauth.facebook.user = foundUser if foundUser
fb.handleAuthCallbackError (req, res) ->
if req.query and req.query.error_reason is 'user_denied'
require('./routes').index req, res
fb.redirectPath '/courtside'
app.use everyauth.middleware app
{
"name": "myLaddr",
"version": "0.0.1",
"description": "A web sports ladder",
"main": "app.js",
"dependencies": {
"coffee-script": "1.3.x",
"express": "3.x",
"jade": "*",
"stylus": "*",
"mongoose": "~3.1.2",
"nodemon": "0.6.23",
"everyauth": "git://github.com/bnoguchi/everyauth.git#express3"
},
"devDependencies": {
"mocha": "~1.4.2",
"chai": "~1.2.0"
},
"repository": {
"type": "git",
"url": "https://github.com/locksmithdon/myladdr.git"
},
"engines": {
"node": "0.8.x"
},
"author": {
"name": "Don Smith",
"email": "[email protected]>",
"url": "http://code.locksmithdon.net"
},
"private": false,
"scripts": {
"start": "nodemon --debug ./app.coffee 3000",
"test": "mocha --reporter spec --compilers coffee:coffee-script"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment