Skip to content

Instantly share code, notes, and snippets.

@MBehtemam
Created February 24, 2014 08:58
Show Gist options
  • Save MBehtemam/9184138 to your computer and use it in GitHub Desktop.
Save MBehtemam/9184138 to your computer and use it in GitHub Desktop.
Koa Local Strategy Auth
var koa = require('koa'),
AuthenticationApp = koa(),
session = require('koa-session'),
passport = require('koa-passport'),
route = require('koa-route'),
Router = require('koa-router'),
hbs = require('koa-hbs'),
render = require('koa-render'),
bodyParser = require('koa-body-parser'),
formidable = require('koa-formidable'),
formidable = require('koa-formidable'),
LocalStrategy = require('passport-local').Strategy;
var user = { id: 1, username: 'admin' }
passport.serializeUser(function(user, done) {
done(null, user.id)
})
passport.deserializeUser(function(id, done) {
done(null, user)
})
passport.use(new LocalStrategy(function(username, password, done) {
// retrieve user ...
console.log("CALLING IDFDSDF");
if (username === 'admin' && password === 'admin') {
console.log("USER");
done(null, user)
} else {
console.log("NO USER");
done(null, false)
}
}));
AuthenticationApp.keys = ['your-session-secret']
AuthenticationApp.use(session())
AuthenticationApp.use(Router(AuthenticationApp));
AuthenticationApp.use(passport.initialize());
AuthenticationApp.use(passport.session());
AuthenticationApp.use(hbs.middleware({
viewPath: __dirname + '/views'
}));
AuthenticationApp.use(bodyParser());
AuthenticationApp.use(formidable());
AuthenticationApp.use(route.get('/',login));
function *login(){
yield this.render('login',{"application":"NAP","message":""});
}
// AuthenticationApp.use(route.post('/login',loginParse));
var public = new Router();
// POST /login
AuthenticationApp.post('/login',
formidable(),
passport.authenticate('local', {
successRedirect: '/selector',
failureRedirect: '/'
})
)
AuthenticationApp.use(function*(next) {
this.req.query = this.query
yield next
})
// Require authentication for now
AuthenticationApp.use(function*(next) {
if (this.req.isAuthenticated()) {
yield next
} else {
this.redirect('/')
}
})
// function *loginParse(next){
// passport.authenticate('local', {
// successRedirect: '/app',
// failureRedirect: '/'
// })
// }
AuthenticationApp.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment