mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-08-05 12:02:13 +00:00
refactor(facebook): use async.auto to collect multiple vars, cleaner
than waterfall
This commit is contained in:
parent
2822ce233f
commit
d91d9e1eb3
1 changed files with 17 additions and 18 deletions
|
|
@ -148,21 +148,19 @@ api.loginSocial = function(req, res, next) {
|
||||||
network = req.body.network;
|
network = req.body.network;
|
||||||
if (network!=='facebook')
|
if (network!=='facebook')
|
||||||
return res.json(401, {err:"Only Facebook supported currently."});
|
return res.json(401, {err:"Only Facebook supported currently."});
|
||||||
async.waterfall([
|
async.auto({
|
||||||
function(cb){
|
profile: function (cb) {
|
||||||
passport._strategies[network].userProfile(access_token, cb);
|
passport._strategies[network].userProfile(access_token, cb);
|
||||||
},
|
},
|
||||||
function(profile, cb) {
|
user: ['profile', function (cb, results) {
|
||||||
var q = {};q['auth.'+network+'.id'] = profile.id;
|
var q = {};
|
||||||
User.findOne(q, {_id:1, apiToken:1, auth:1}, function(err, user){
|
q['auth.' + network + '.id'] = results.profile.id;
|
||||||
if (err) return cb(err);
|
User.findOne(q, {_id: 1, apiToken: 1, auth: 1}, cb);
|
||||||
cb(null, {user:user, profile:profile});
|
}],
|
||||||
});
|
register: ['profile', 'user', function (cb, results) {
|
||||||
},
|
if (results.user) return cb(null, results.user);
|
||||||
function(data, cb){
|
|
||||||
if (data.user) return cb(null, data.user);
|
|
||||||
// Create new user
|
// Create new user
|
||||||
var prof = data.profile;
|
var prof = results.profile;
|
||||||
var user = {
|
var user = {
|
||||||
preferences: {
|
preferences: {
|
||||||
language: req.language // User language detected from browser, not saved
|
language: req.language // User language detected from browser, not saved
|
||||||
|
|
@ -175,15 +173,16 @@ api.loginSocial = function(req, res, next) {
|
||||||
user = new User(user);
|
user = new User(user);
|
||||||
user.save(cb);
|
user.save(cb);
|
||||||
|
|
||||||
if(isProd && prof.emails && prof.emails[0] && prof.emails[0].value){
|
if (isProd && prof.emails && prof.emails[0] && prof.emails[0].value) {
|
||||||
utils.txnEmail({name:prof.displayName || prof.username, email:prof.emails[0].value}, 'welcome');
|
utils.txnEmail({name: prof.displayName || prof.username, email: prof.emails[0].value}, 'welcome');
|
||||||
}
|
}
|
||||||
ga.event('register', network).send();
|
ga.event('register', network).send();
|
||||||
}
|
}]
|
||||||
], function(err, user){
|
}, function(err, results){
|
||||||
if (err) return res.json(401, {err: err.toString ? err.toString() : err});
|
if (err) return res.json(401, {err: err.toString ? err.toString() : err});
|
||||||
if (user.auth.blocked) return res.json(401, accountSuspended(user._id));
|
var acct = results.register[0] ? results.register[0] : results.register;
|
||||||
return res.json(200, {id: user.id, token:user.apiToken});
|
if (acct.auth.blocked) return res.json(401, accountSuspended(acct._id));
|
||||||
|
return res.json(200, {id:acct._id, token:acct.apiToken});
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue