Skip to content

Instantly share code, notes, and snippets.

@scan

scan/app.coffee Secret

Created August 11, 2012 08:27
Show Gist options
  • Save scan/dd5e39051f278b307cc0 to your computer and use it in GitHub Desktop.
Save scan/dd5e39051f278b307cc0 to your computer and use it in GitHub Desktop.
express = require 'express'
MongooseStore = (require 'connect-mongoose')(express)
mongoose = require 'mongoose'
passport = require 'passport'
app = express()
MONTH = 1000 * 60 * 60 * 24 * 30
ASSET_DIR = "#{__dirname}/assets"
PUBLIC_DIR = "#{__dirname}/public"
VIEW_DIR = "#{__dirname}/../views"
require './model'
mongoose.connection.on 'open', ->
global.app = app
require './auth'
app.configure ->
app.set 'view engine', 'jade'
app.set 'views', VIEW_DIR
app.use require('connect-assets')()
app.use express.bodyParser()
app.use express.cookieParser 'super secret'
app.use express.session
cookie:
httpOnly: yes
path: '/'
maxAge: MONTH
store: new MongooseStore()
app.use express.errorHandler()
app.use express.methodOverride()
app.use express.csrf()
app.use express.favicon()
app.use passport.initialize()
app.use passport.session()
app.use (req, res, next) ->
res.locals.url = req.url
res.locals.user = req.user
#res.locals.authenticated = req.isAuthenticated()
res.locals.csrf = req.session._csrf
next()
app.use app.router
app.configure 'development', ->
app.use express.logger ':method :url - :referrer'
app.use express.static PUBLIC_DIR
app.configure 'production', ->
app.use express.static PUBLIC_DIR, maxAge: MONTH
app.use express.compress()
app.get '/', (req, res) ->
res.render 'index'
app.listen process.env.app_port or 8080
console.log "Started server in #{app.settings.env} mode."
mongoose.connect (require './config')['mongo url']
mongoose = require 'mongoose'
passport = require 'passport'
{User} = require './model'
config = require './config'
app = global.app
LocalStrategy = (require 'passport-local').Strategy
GoogleStrategy = (require 'passport-google-oauth').OAuth2Strategy
passport.serializeUser (user, done) ->
done null, user.id
passport.deserializeUser (id, done) ->
User.findOne id, done
passport.use new GoogleStrategy
clientID: config.google['client id']
clientSecret: config.google['client secret']
callbackURL: "#{config.host}auth/google/callback"
, (accessToken, refreshToken, profile, done) ->
console.log profile
User.findByLogin 'google', profile.id, (err, user) ->
if err? or user? then done err, user
else
done null,
new: yes
service: 'google'
id: profile.id
email: profile.email
name: profile.name
social_profiles: [service: 'google+', url: profile.link] if profile.link?
app.get '/auth/google', passport.authenticate('google', scope: ['https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/userinfo.email']), (req, res) ->
app.get '/auth/google/callback', passport.authenticate('google', failureRedirect: '/login'), (req, res) ->
console.log req.user
res.send req.user
exports =
ensureAuthenticated: (req, res, next) -> if req.isAuthenticated() then next() else res.redirect '/login'
requireAuthenticated: (req, res, next) -> if req.isAuthenticated() then next() else res.send 401, error: 'Not logged in'
{
"name": "ponyfolder",
"version": "0.0.1",
"private": true,
"node": "0.8.x",
"dependencies": {
"cloudfiles": "",
"coffee-script": "1.3.x",
"connect": "2.4.2",
"connect-assets": "",
"connect-mongoose": "",
"express": "3.x",
"gm": "",
"gravatar": "",
"jade": "",
"less": "",
"moment": "",
"mongoose": "3.x",
"mongoose-types": "",
"nib": "",
"oauth2orize": "",
"passport": "",
"passport-facebook": "",
"passport-google-oauth": "",
"passport-local": "",
"passport-tumblr": "",
"passport-twitter": "",
"socket.io": "",
"stylus": "",
"underscore": "",
"underscore.string": ""
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment