-
-
Save francisbrito/e82377e700823cd0cc75 to your computer and use it in GitHub Desktop.
Koa, mongoskin, monk, koa-ejs, koa-passport
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 koa = require('koa'); | |
var logger = require('koa-logger'); | |
var path = require('path'); | |
var Router = require('koa-router'); | |
var bodyParser = require('koa-bodyparser'); | |
var render = require('koa-ejs'); | |
var session = require('koa-generic-session'); | |
var passport = require('koa-passport'); | |
var monk = require('monk'); //it's a wrapper for mongoskin :) | |
var wrap = require('co-monk'); //it's a wrapper for monk :) | |
var db = monk("mongodb://localhost:27017/todo"); | |
//var db=monk(process.env.MONGOHQ_URL,{w:1}); | |
//for heroku.com enviroment variable. | |
var busers = db.get('users'); | |
//for passport-local, koa-passport with callbacks hell. | |
//Here I don't understand how to handle this all about with koa-passport.js | |
// co? or function *(){yield something?? | |
//But an old good callback, so I feel better :) | |
var us = wrap(db.get('users'); //for a test in routing system with generators. | |
passport.serializeUser(function(user, done) { | |
done(null, user._id); | |
}); | |
passport.deserializeUser(function(_id, done) { | |
busers.findById(_id, function(err, user) { | |
if (err) { | |
return done(err); | |
} | |
done(null, user); | |
}); | |
}); | |
var LocalStrategy = require('passport-local').Strategy; | |
passport.use(new LocalStrategy( | |
function(username, password, done) { | |
process.nextTick(function() { | |
busers.findOne({ | |
'username': username | |
}, function(err, user) { | |
if (err) { | |
return done(err); | |
} | |
if (!user) { | |
return done(null, false, { | |
message: 'Unknown user ' + username | |
}); | |
} | |
if (user.password != password) { | |
return done(null, false, { | |
message: 'Invalid password' | |
}); | |
} | |
return done(null, user); | |
}); | |
}); | |
})); | |
var app = koa() | |
; app.use(session()); app.use(logger()); app.keys = ['your-session-secret-for-Putin-and-Co']; | |
app.use(bodyParser()); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
app.use(Router(app)); | |
var locals = { | |
version: '0.0.1' | |
}; | |
render(app, { | |
root: path.join(__dirname, 'views'), | |
layout: 'template', | |
viewExt: 'html', | |
cache: false, | |
debug: true, | |
locals: locals, | |
filters: 1 | |
}); | |
var public = new Router(); | |
public.get('/', function*() { | |
var user = yield us.find({ | |
'username': 'Bob' | |
}); | |
console.log(user); | |
var body = this.req.user; | |
yield this.render('login', { | |
user: body | |
}); | |
}); | |
public.post('/login', | |
passport.authenticate('local', { | |
successRedirect: '/app', | |
failureRedirect: '/' | |
}) | |
); | |
public.get('/logout', function*(next) { | |
this.logout(); | |
this.redirect('/'); | |
}); | |
app.use(public.middleware()); | |
function* authed(next) { | |
if (this.req.isAuthenticated()) { | |
yield next; | |
} else { | |
this.redirect('/'); | |
} | |
} | |
var secured = new Router(); | |
secured.get('/admin', authed, function*() { | |
var body = this.req.user.username; | |
console.log('this.req.user.username in admin: ' + body); | |
yield this.render('app'); | |
}); | |
app.use(secured.middleware()) | |
; | |
app.listen(process.env.PORT || 3000); | |
console.log(3000); | |
//iojs server |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment