From 8d7de6fe618aabf7329573efd7f7c2988b996e20 Mon Sep 17 00:00:00 2001 From: Andreas Richter Date: Sat, 13 Oct 2012 15:36:35 -0400 Subject: [PATCH] Finish twitter login and fix name mixup between facebook and twitter logins. --- app.js | 38 +++++++++++++++++++++++--------------- modules/core/user/user.js | 10 +++++----- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/app.js b/app.js index dec5ad3a4..8141c55bc 100644 --- a/app.js +++ b/app.js @@ -127,23 +127,26 @@ everyauth.everymodule }); }); -function calipsoFindOrCreateUser(username, promise) { +function calipsoFindOrCreateUser(user, sess, promise) { var User = calipso.db.model('User'); - function finishUser(sess, user) { - if (!sess._pending) return promise.fulfill(user); - return calipso.lib.user.createUserSession(sess._pending, null, user, function(err) { - if(err) { calipso.error("Error saving session: " + err); return promise.fail(err); } + function finishUser(user) { + if (sess) { + if (!sess._pending) return promise.fulfill(user); + return calipso.lib.user.createUserSession(sess._pending, null, user, function(err) { + if(err) { calipso.error("Error saving session: " + err); return promise.fail(err); } + promise.fulfill(user); + }); + } else promise.fulfill(user); - }); } - User.findOne({username:username}, function (err, user) { + User.findOne({username:user.username}, function (err, u) { if (err) return promise.fail(err); - if (user) return promise.fulfill(user); - var u = new User({ - username: 'google:' + googleUser.email, - fullname: googleUser.name, - email: googleUser.email, + if (u) return promise.fulfill(u); + u = new User({ + username: user.username, + fullname: user.name, + email: user.email, hash: 'external:auth' }); u.roles = ['Guest']; // Todo - need to make sure guest role can't be deleted? @@ -154,7 +157,7 @@ function calipsoFindOrCreateUser(username, promise) { if (err) return promise.fail(err); calipso.e.post_emit('USER_CREATE',u); // If not already redirecting, then redirect - finishUser(sess, u); + finishUser(u); }); }); return promise; @@ -233,10 +236,14 @@ function bootApplication(next) { calipso.auth.twitter = true; everyauth .twitter + .apiHost('https://api.twitter.com/1') .consumerKey(consumerKey) .consumerSecret(consumerSecret) .findOrCreateUser( function (sess, accessToken, accessSecret, twitUser) { - console.log(twitUser); + var promise = this.Promise(); + + return calipsoFindOrCreateUser({username:'twitter:' + twitUser.screen_name, + email:twitUser.screen_name + '@twitter.com', name:twitUser.name}, sess, promise); }) .redirectPath('/'); } @@ -260,7 +267,8 @@ function bootApplication(next) { var promise = this.Promise(); - return calipsoFindOrCreateUser('google:' + googleUser.email, promise); + return calipsoFindOrCreateUser({username:'google:' + googleUser.email, + email:googleUser.email, name:googleUser.name}, sess, promise); }) .redirectPath('/'); } diff --git a/modules/core/user/user.js b/modules/core/user/user.js index 89e36c03b..abc2fefe6 100644 --- a/modules/core/user/user.js +++ b/modules/core/user/user.js @@ -175,14 +175,14 @@ function userFields() { {label:'Username', name:'user[username]', type:'text'}, {label:'Password', name:'user[password]', type:'password'} ]; - if (true /* enable google */) { + if (calipso.auth.google) { fields.push({name:'google',text:'Use Google Login', type:'link', href:'/auth/google', cls:'googleicon'}); } - if (true /* enable twitter */) { - fields.push({name:'twitter',text:'Use Twitter Login', type:'link', href:'/auth/facebook', cls:'facebookicon'}); + if (calipso.auth.twitter) { + fields.push({name:'twitter',text:'Use Twitter Login', type:'link', href:'/auth/twitter', cls:'twittericon'}); } - if (true /* enable facebook */) { - fields.push({name:'facebook',text:'Use Facebook Login', type:'link', href:'/auth/twitter', cls:'twittericon'}); + if (calipso.auth.facebook) { + fields.push({name:'facebook',text:'Use Facebook Login', type:'link', href:'/auth/facebook', cls:'facebookicon'}); } return fields; }