diff options
author | Dennis Eriksen <dennis.se@gmail.com> | 2013-04-18 14:18:22 +0200 |
---|---|---|
committer | Dennis Eriksen <dennis.se@gmail.com> | 2013-04-18 14:18:22 +0200 |
commit | 8c18de44260a3eab4b5a1c61e29691eae84ecec9 (patch) | |
tree | 40e6061489ddfb964c5d19369d473b1df8ea887d /modules | |
parent | changing to passport-local (diff) | |
download | Divid-8c18de44260a3eab4b5a1c61e29691eae84ecec9.tar.gz |
changing to passport for authentication
Diffstat (limited to 'modules')
-rw-r--r-- | modules/passport-local.js | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/modules/passport-local.js b/modules/passport-local.js index 939873f..b269707 100644 --- a/modules/passport-local.js +++ b/modules/passport-local.js @@ -1,3 +1,8 @@ +var mongoose = require('mongoose') + , passport = require('passport') + , LocalStrategy = require('passport-local').Strategy + , SALT_WORK_FACTOR = 10; + mongoose.connect('localhost', 'test'); @@ -7,6 +12,7 @@ db.once('open', function callback() { console.log('Connected to MongoDB'); }) + // User schema var userSchema = mongoose.Schema({ username: { type: String, required: true, unique: true }, @@ -20,7 +26,7 @@ userSchema.pre('save', function(next) { if (!user.isModified('password')) return next(); - bcrypt genSalt(SALT_WORK_FACTOR, function(err, salt) { + bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) { if (err) return next(err); bcrypt.hash(user.password, salt, function(err, hash) { @@ -47,11 +53,11 @@ userSchema.methods.comparePassword = function(candidatePassword, callback) { // serialize users into and deserialize users out of the session. Typically, // this will be as simple as storing the user ID when serializing, and finding // the user by ID when deserializing. -exports.passport.serializeUser(function(user, done) { +passport.serializeUser(function(user, done) { done(null, user.id); }); -exports.passport.deserializeUser(function(id, done) { +passport.deserializeUser(function(id, done) { User.findById(id, function (err, user) { done(err, user); }); @@ -63,7 +69,7 @@ exports.passport.deserializeUser(function(id, done) { // credentials (in this case, a username and password), and invoke a callback // with a user object. In the real world, this would query a database; // however, in this example we are using a baked-in set of users. -exports.passport.use(new LocalStrategy(function(username, password, done) { +passport.use(new LocalStrategy(function(username, password, done) { User.findOne({ username: username }, function(err, user) { if (err) return done(err); if (!user) { return done(null, false, { message: 'Unknown user ' + username }); } |