From 33f8ee57bf626ed6c53fdff042a1e310195c0f87 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 24 May 2013 12:12:59 -0400 Subject: [PATCH] revamped social auth code so that accounts are merged if they share a common email also updated auth code so that fbid gplusid and twid are saved in the hash instead of as simple key->value --- src/user.js | 58 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/src/user.js b/src/user.js index a600fbf9e9..f3abd8510a 100644 --- a/src/user.js +++ b/src/user.js @@ -196,8 +196,8 @@ var config = require('../config.js'), callback(err); } else { // Save twitter-specific information to the user - RDB.set('uid:' + uid + ':twid', twid); - RDB.set('twid:' + twid + ':uid', uid); + User.setUserField(uid, 'twid', twid); + RDB.hset('twid:uid', twid, uid); callback(null, { uid: uid }); @@ -216,17 +216,23 @@ var config = require('../config.js'), }); } else { // New User - User.create(handle, null, email, function(err, uid) { - if (err !== null) { - callback(err); - } else { - // Save twitter-specific information to the user - RDB.set('uid:' + uid + ':gplusid', gplusid); - RDB.set('gplusid:' + gplusid + ':uid', uid); - callback(null, { - uid: uid + 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) { + User.create(handle, null, email, function(err, uid) { + if (err !== null) { + callback(err); + } else success(uid); }); - } + } else success(uid); // Existing account -- merge }); } }); @@ -241,17 +247,23 @@ var config = require('../config.js'), }); } else { // New User - User.create(name, null, email, function(err, uid) { - if (err !== null) { - callback(err); - } else { - // Save twitter-specific information to the user - RDB.set('uid:' + uid + ':fbid', fbid); - RDB.set('fbid:' + fbid + ':uid', uid); + 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) { + User.create(name, null, email, function(err, uid) { + if (err !== null) { + callback(err); + } else success(uid); + }); + } else success(uid); // Existing account -- merge }); } }); @@ -493,21 +505,21 @@ var config = require('../config.js'), }; User.get_uid_by_twitter_id = function(twid, callback) { - RDB.get('twid:' + twid + ':uid', function(err, uid) { + RDB.hget('twid:uid', twid, function(err, uid) { RDB.handle(err); callback(uid); }); } User.get_uid_by_google_id = function(gplusid, callback) { - RDB.get('gplusid:' + gplusid + ':uid', function(err, uid) { + RDB.hget('gplusid:uid', gplusid, function(err, uid) { RDB.handle(err); callback(uid); }); } User.get_uid_by_fbid = function(fbid, callback) { - RDB.get('fbid:' + fbid + ':uid', function(err, uid) { + RDB.hget('fbid:uid', fbid, function(err, uid) { RDB.handle(err); callback(uid); });