Created
June 13, 2014 14:58
-
-
Save debrouwere/a46dec73629c71706809 to your computer and use it in GitHub Desktop.
Express 4 + PassportJS
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
<form action="/login" method="post"> | |
<div> | |
<label>Username:</label> | |
<input type="text" name="username" /> | |
</div> | |
<div> | |
<label>Password:</label> | |
<input type="password" name="password" /> | |
</div> | |
<div> | |
<input type="submit" value="Log In"/> | |
</div> | |
</form> |
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
// brew install scrypt | |
// npm install express body-parser cookie-parser scrypt passport passport-local sequelize sqlite3 | |
var fs = require('fs'); | |
var url = require('url'); | |
var scrypt = require('scrypt'); | |
var Sequelize = require('sequelize'); | |
var express = require('express'); | |
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; | |
scrypt.hash.config.keyEncoding = 'utf8' | |
scrypt.hash.config.outputEncoding = 'hex' | |
scrypt.verify.config.keyEncoding = 'utf8' | |
scrypt.verify.config.hashEncoding = 'hex' | |
sequelize = new Sequelize('passport', null, null, { | |
dialect: "sqlite", | |
storage: "passport.sqlite", | |
}); | |
var Organization = sequelize.define('Organization', { | |
domain: { | |
type: Sequelize.TEXT, | |
primaryKey: true, | |
set: function(value) { | |
this.setDataValue('domain', Organization.parseDomain(value)); | |
} | |
}, | |
shortURL: { | |
type: Sequelize.TEXT, | |
}, | |
name: { | |
type: Sequelize.TEXT, | |
}, | |
password: { | |
type: Sequelize.TEXT, | |
set: function(value) { | |
hash = scrypt.hash(value, scrypt.params(0.1)); | |
this.setDataValue('password', hash); | |
} | |
}, | |
apiKey: { | |
type: Sequelize.UUIDV4, | |
} | |
}, { | |
instanceMethods: { | |
verifyPassword: function(attempt) { | |
if (scrypt.verify(this.password, attempt)) { | |
return this; | |
} else { | |
return false; | |
} | |
}, | |
}, | |
classMethods: { | |
verify: function(domain, password, callback) { | |
domain = Organization.parseDomain(domain); | |
Organization.find({where: {domain: domain}}) | |
.success(function(organization){ | |
if (organization) { | |
callback(null, organization.verifyPassword(password)); | |
} else { | |
callback(null, false); | |
} | |
}) | |
}, | |
parseDomain: function(value) { | |
if (value.substr(0, 4) != 'http') { | |
value = 'http://' + value; | |
} | |
return url.parse(value).hostname; | |
} | |
}, | |
}); | |
passport.use(new LocalStrategy(Organization.verify)); | |
/* Passport requires some configuration in order to allow for | |
persistent sessions (using cookies). */ | |
passport.serializeUser(function(organization, done) { | |
done(null, organization.domain); | |
}); | |
passport.deserializeUser(function(domain, done) { | |
Organization.find({where: {domain: domain}}).success(function(organization) { | |
done(null, organization); | |
}); | |
}); | |
var app = express(); | |
app.use(cookieParser()); | |
app.use(bodyParser.urlencoded()); | |
app.use(session({secret: 'keyboard cat'})); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
var authenticate = passport.authenticate('local', { | |
successRedirect: '/', | |
failureRedirect: '/login', | |
}); | |
app.get('/', function (req, res) { | |
if (req.isAuthenticated()) { | |
res.send('Status: logged in as ', req.user.domain); | |
} else { | |
res.send('Status: not currently logged in.') | |
} | |
}); | |
app.get('/login', function(req, res){ | |
res.sendfile('passport.html'); | |
}) | |
app.post('/login', authenticate); | |
var dummyOrganization = { | |
name: 'NewsLynx', | |
domain: 'http://newslynx.org', | |
password: 'guacamole' | |
} | |
function initialize () { | |
Organization | |
.create(dummyOrganization) | |
.success(function(user, created) { | |
app.listen(3000); | |
}); | |
} | |
sequelize.sync({force: true}) | |
.success(initialize) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment