Last active
December 28, 2015 14:46
-
-
Save emacsen/b11e4afce7bfe7841bdf to your computer and use it in GitHub Desktop.
This file contains hidden or 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'); | |
var nunjucks = require('nunjucks'); | |
var bodyParser = require('body-parser'); | |
var cookieParser = require('cookie-parser') | |
var session = require('express-session') | |
var passport = require('passport'); | |
var LocalStrategy = require('passport-local').Strategy; | |
var db = require('./db'); | |
passport.use(new LocalStrategy( | |
function(username, password, cb) { | |
console.log("Hi"); | |
db.findByUsername(username, function(err, user) { | |
if (err) { | |
console.log("Some weird error: " + err); | |
return cb(err); | |
} | |
if (!user) { | |
console.log("User doesn't exist: " + username); | |
return cb(null, false); | |
} | |
if (user.password != password) { | |
console.log("Incorrect password"); | |
return cb(null, false); | |
} | |
console.log("Successful login"); | |
console.log(user); | |
return cb(null, user); | |
}); | |
})); | |
passport.serializeUser(function(user, cb) { | |
console.log("User serialized " + user.id); | |
cb(null, user.id); | |
}); | |
passport.deserializeUser(function(id, cb) { | |
console.log("User deserailized " + id); | |
db.findById(id, function (err, user) { | |
if (err) { return cb(err); } | |
cb(null, user); | |
}); | |
}); | |
// Create a new Express application. | |
var app = express(); | |
nunjucks.configure('templates', { | |
autoescape: true, | |
express : app | |
}); | |
app.use(cookieParser()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(bodyParser.json()); | |
app.use(session({secret: 'keyboard cat', resave: false, saveUninitialized: false })); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
// Define routes. | |
app.get('/', | |
function(req, res) { | |
console.log("User"); | |
console.log(req.user); | |
res.render('home.nunjucks', { user: req.user }); | |
}); | |
app.get('/login', | |
function(req, res){ | |
res.render('login.nunjucks'); | |
}); | |
app.post('/login', | |
passport.authenticate('local', { | |
session: true, | |
failureRedirect: '/login', | |
successRedirect: '/' | |
}) | |
); | |
app.get('/logout', | |
function(req, res){ | |
req.logout(); | |
res.redirect('/'); | |
}); | |
app.get('/profile', | |
require('connect-ensure-login').ensureLoggedIn(), | |
function(req, res){ | |
console.log("User"); | |
console.log(req.user); | |
res.render('profile.nunjucks', { user: req.user }); | |
}); | |
This file contains hidden or 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 records = [ | |
{ id: 1, username: 'jack', password: 'secret', displayName: 'Jack', emails: [ { value: '[email protected]' } ] } | |
, { id: 2, username: 'jill', password: 'birthday', displayName: 'Jill', emails: [ { value: '[email protected]' } ] } | |
]; | |
exports.findById = function(id, cb) { | |
process.nextTick(function() { | |
var idx = id - 1; | |
if (records[idx]) { | |
cb(null, records[idx]); | |
} else { | |
cb(new Error('User ' + id + ' does not exist')); | |
} | |
}); | |
} | |
exports.findByUsername = function(username, cb) { | |
process.nextTick(function() { | |
for (var i = 0, len = records.length; i < len; i++) { | |
var record = records[i]; | |
if (record.username === username) { | |
return cb(null, record); | |
} | |
} | |
return cb(null, null); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment