Files
NodeBB/src/login.js

156 lines
3.7 KiB
JavaScript
Raw Normal View History

var user = require('./user.js'),
bcrypt = require('bcrypt'),
RDB = require('./redis.js'),
2013-08-13 15:25:40 -04:00
path = require('path'),
2013-10-25 18:08:23 -04:00
winston = require('winston'),
utils = require('./../public/src/utils.js');
2013-09-17 13:09:37 -04:00
(function(Login) {
Login.loginViaLocal = function(username, password, next) {
if (!username || !password) {
return next({
status: 'error',
message: 'invalid-user'
});
} else {
2013-10-25 18:08:23 -04:00
var userslug = utils.slugify(username);
user.get_uid_by_userslug(userslug, function(err, uid) {
2013-10-25 18:09:07 -04:00
2013-09-15 10:11:29 -04:00
if (err) {
return next(new Error('redis-error'));
2013-10-25 18:09:07 -04:00
} else if (uid == null) {
2013-09-15 10:11:29 -04:00
return next(new Error('invalid-user'));
}
2013-08-23 13:14:36 -04:00
user.getUserFields(uid, ['password', 'banned'], function(err, userData) {
2013-09-17 13:09:37 -04:00
if (err) return next(err);
2013-08-23 13:14:36 -04:00
2013-09-17 13:09:37 -04:00
if (userData.banned && userData.banned === '1') {
return next({
status: "error",
message: "user-banned"
});
}
2013-08-23 13:14:36 -04:00
bcrypt.compare(password, userData.password, function(err, res) {
2013-09-17 13:09:37 -04:00
if (err) {
winston.err(err.message);
2013-09-15 10:11:29 -04:00
next(new Error('bcrypt compare error'));
2013-07-06 00:00:38 -04:00
return;
}
2013-08-23 13:14:36 -04:00
2013-07-08 12:10:21 -04:00
if (res) {
2013-09-17 13:09:37 -04:00
next(null, {
user: {
uid: uid
}
});
} else {
2013-09-15 10:11:29 -04:00
next(new Error('invalid-password'));
}
});
});
});
}
}
Login.loginViaTwitter = function(twid, handle, photos, callback) {
user.get_uid_by_twitter_id(twid, function(uid) {
if (uid !== null) {
// Existing User
callback(null, {
uid: uid
});
} else {
// New User
2013-07-24 12:04:32 -04:00
user.create(handle, undefined, undefined, function(err, uid) {
if (err !== null) {
callback(err);
} else {
// Save twitter-specific information to the user
user.setUserField(uid, 'twid', twid);
RDB.hset('twid:uid', twid, uid);
// Save their photo, if present
if (photos && photos.length > 0) {
2013-09-17 13:09:37 -04:00
var photoUrl = photos[0].value;
photoUrl = path.dirname(photoUrl) + '/' + path.basename(photoUrl, path.extname(photoUrl)).slice(0, -6) + 'bigger' + path.extname(photoUrl);
user.setUserField(uid, 'uploadedpicture', photoUrl);
user.setUserField(uid, 'picture', photoUrl);
}
callback(null, {
uid: uid
});
}
});
}
});
}
Login.loginViaGoogle = function(gplusid, handle, email, callback) {
user.get_uid_by_google_id(gplusid, function(uid) {
if (uid !== null) {
// Existing User
callback(null, {
uid: uid
});
} else {
// New User
var success = function(uid) {
// Save google-specific information to the user
user.setUserField(uid, 'gplusid', gplusid);
RDB.hset('gplusid:uid', gplusid, uid);
callback(null, {
uid: uid
});
}
user.get_uid_by_email(email, function(uid) {
if (!uid) {
2013-07-24 12:04:32 -04:00
user.create(handle, undefined, email, function(err, uid) {
if (err !== null) {
callback(err);
} else success(uid);
});
} else success(uid); // Existing account -- merge
});
}
});
}
Login.loginViaFacebook = function(fbid, name, email, callback) {
user.get_uid_by_fbid(fbid, function(uid) {
if (uid !== null) {
// Existing User
callback(null, {
uid: uid
});
} else {
// New User
var success = function(uid) {
// Save facebook-specific information to the user
user.setUserField(uid, 'fbid', fbid);
RDB.hset('fbid:uid', fbid, uid);
callback(null, {
uid: uid
});
}
user.get_uid_by_email(email, function(uid) {
if (!uid) {
2013-07-24 12:04:32 -04:00
user.create(name, undefined, email, function(err, uid) {
if (err !== null) {
callback(err);
} else success(uid);
});
} else success(uid); // Existing account -- merge
});
}
});
}
2013-09-17 13:09:37 -04:00
}(exports));