diff --git a/public/src/forum/login.js b/public/src/forum/login.js index d99cc5c652..1f4df45ea6 100644 --- a/public/src/forum/login.js +++ b/public/src/forum/login.js @@ -18,29 +18,23 @@ define(function() { url: RELATIVE_PATH + '/login', data: loginData, success: function(data, textStatus, jqXHR) { + $('#login').html('Redirecting...'); + if(!app.previousUrl) { + app.previousUrl = '/'; + } - if (!data.success) { - $('#login-error-notify').show(); - $('#login').removeAttr('disabled').html('Login'); + if(app.previousUrl.indexOf('/reset/') !== -1) { + window.location.replace(RELATIVE_PATH + "/?loggedin"); } else { - $('#login').html('Redirecting...'); - if(!app.previousUrl) { - app.previousUrl = '/'; - } - - if(app.previousUrl.indexOf('/reset/') !== -1) { - window.location.replace(RELATIVE_PATH + "/?loggedin"); + var index = app.previousUrl.indexOf('#'); + if(index !== -1) { + window.location.replace(app.previousUrl.slice(0, index) + '?loggedin' + app.previousUrl.slice(index)); } else { - var index = app.previousUrl.indexOf('#'); - if(index !== -1) { - window.location.replace(app.previousUrl.slice(0, index) + '?loggedin' + app.previousUrl.slice(index)); - } else { - window.location.replace(app.previousUrl + "?loggedin"); - } + window.location.replace(app.previousUrl + "?loggedin"); } - - app.loadConfig(); } + + app.loadConfig(); }, error: function(data, textStatus, jqXHR) { $('#login-error-notify').show(); diff --git a/src/routes/authentication.js b/src/routes/authentication.js index 2476b391f2..5f40c1133b 100644 --- a/src/routes/authentication.js +++ b/src/routes/authentication.js @@ -13,16 +13,6 @@ login_strategies = []; - passport.use(new passportLocal(function(user, password, next) { - Auth.login(user, password, function(err, login) { - if (!err) { - next(null, login.user); - } else { - next(null, false, err); - } - }); - })); - plugins.ready(function() { plugins.fireHook('filter:auth.init', login_strategies, function(err) { if (err) { @@ -33,16 +23,6 @@ }); }); - passport.serializeUser(function(user, done) { - done(null, user.uid); - }); - - passport.deserializeUser(function(uid, done) { - done(null, { - uid: uid - }); - }); - Auth.initialize = function(app) { app.use(passport.initialize()); app.use(passport.session()); @@ -107,11 +87,9 @@ if (err) { return next(err); } + if (!user) { - return res.send({ - success: false, - message: info.message - }); + return res.json(403, info); } // Alter user cookie depending on passed-in option @@ -127,10 +105,7 @@ req.login({ uid: user.uid }, function() { - res.send({ - success: true, - message: 'authentication succeeded' - }); + res.json(info); }); })(req, res, next); }); @@ -163,50 +138,60 @@ Auth.login = function(username, password, next) { if (!username || !password) { - return next({ - status: 'error', - message: 'invalid-user' - }); - } else { + return next(new Error('invalid-user')); + } + + var userslug = utils.slugify(username); + + user.getUidByUserslug(userslug, function(err, uid) { + if (err) { + return next(err); + } - var userslug = utils.slugify(username); + if(!uid) { + return next(null, false, 'user doesn\'t exist'); + } - user.getUidByUserslug(userslug, function(err, uid) { + user.getUserFields(uid, ['password', 'banned'], function(err, userData) { if (err) { - return next(new Error('redis-error')); - } else if (uid == null) { - return next(new Error('invalid-user')); + return next(err); } - user.getUserFields(uid, ['password', 'banned'], function(err, userData) { - if (err) return next(err); + if (!userData || !userData.password) { + return next(new Error('invalid userdata or password')); + } - if (userData.banned && parseInt(userData.banned, 10) === 1) { - return next({ - status: "error", - message: "user-banned" - }); + if (userData.banned && parseInt(userData.banned, 10) === 1) { + return next(null, false, 'User banned'); + } + + bcrypt.compare(password, userData.password, function(err, res) { + if (err) { + winston.err(err.message); + return next(new Error('bcrypt compare error')); } - bcrypt.compare(password, userData.password, function(err, res) { - if (err) { - winston.err(err.message); - next(new Error('bcrypt compare error')); - return; - } - - if (res) { - next(null, { - user: { - uid: uid - } - }); - } else { - next(new Error('invalid-password')); - } - }); + if (!res) { + next(null, false, 'invalid-password'); + } + + next(null, { + uid: uid + }, 'Authentication successful'); }); }); - } + }); } + + passport.use(new passportLocal(Auth.login)); + + passport.serializeUser(function(user, done) { + done(null, user.uid); + }); + + passport.deserializeUser(function(uid, done) { + done(null, { + uid: uid + }); + }); }(exports)); \ No newline at end of file