diff options
Diffstat (limited to '')
-rw-r--r-- | app.js | 2 | ||||
-rw-r--r-- | modules/passport-local.js | 14 | ||||
-rw-r--r-- | package.json | 1 | ||||
-rw-r--r-- | router.js | 69 |
4 files changed, 27 insertions, 59 deletions
@@ -10,8 +10,6 @@ var express = require('express') var app = express(); // initiates express - - /** * App configuration */ 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 }); } diff --git a/package.json b/package.json index d08311c..793d239 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "moment": "*", "less-middleware": "*", "passport": "*", + "passport-local": "*", "bcrypt": "*" } } @@ -1,5 +1,6 @@ -var passlo = require('./modules/passport-local'); -var EM = require('./modules/email-dispatcher'); +var passlo = require('./modules/passport-local') + , passport = require('passport') + , EM = require('./modules/email-dispatcher'); @@ -13,19 +14,7 @@ module.exports = function(app) { */ app.get('/', function(req, res){ - // check if the user's credentials are saved in a cookie - if (req.cookies.user == undefined || req.cookies.pass == undefined) { - res.render('index', { title: 'DERS' }); - } else { // appempt automatic login - AM.autoLogin(req.cookies.user, req.cookies.pass, function(o) { - if (o != null) { - req.session.user = o; - res.redirect('/dashboard'); - } else { - res.render('index', { title: 'DERS' }); - } - }); - } + res.render('index', { title: 'DERS' }); }); /* @@ -49,27 +38,10 @@ module.exports = function(app) { */ app.get('/dashboard', function(req, res) { - if (req.session.user == undefined) { - if (req.cookies.user == undefined || req.cookies.pass == undefined) { - res.redirect('/login'); - } else { - console.log('jeg er inne i elsen'); - AM.autoLogin(req.cookies.user, req.cookies.pass, function(o) { - if (o != null) { - req.session.user = o; - res.redirect('/IJUSTAUTOLOGGEDDEGINN'); - } else { - res.redirect('/'); - } - }); - } - // res.redirect('/HAHAHAHAHAHA'); - } else { res.render('dashboard', { title: 'kanin', loggedin: true }); - } }); @@ -81,34 +53,25 @@ module.exports = function(app) { */ app.get('/login', function(req, res) { - if (req.cookies.user == undefined || req.cookies.pass == undefined) { res.render('login', { title: 'Logg inn' }); - } else { - AM.autoLogin(req.cookies.user, req.cookies.pass, function(o) { - if (o != null) { - req.session.user = o; - res.redirect('/dashboard'); - } else { res.render('login', { title: 'Logg inn' }); } - }); - } }); /* POST */ - app.post('/login', function(req, res) { - AM.manualLogin(req.param('user'), req.param('pass'), function(e, o) { - if (!o) { - res.send(e, 400); - } else { - req.session.user = o; - if (req.param('remember-me') == 'on') { - res.cookie('user', o.user, { maxAge: 900000000 }); - res.cookie('pass', o.pass, { maxAge: 900000000 }); - } - res.redirect('/dashboard'); + app.post('/login', function(req, res, next) { + passport.authenticate('local', function(err, user, info) { + if (err) return next(err); + if (!user) { + console.log(info.message); + req.session.messages = [info.message]; + return res.redirect('/login'); } - }); + req.logIn(user, function(err) { + if (err) return next(err); + return res.redirect('/dashboard'); + }) + })(req, res, next); }); |