From 97cb9e154539e66d81ac8f0407e2513a3e4aa063 Mon Sep 17 00:00:00 2001
From: Neil Lalonde <neillalonde@gmail.com>
Date: Wed, 20 Nov 2013 14:22:51 -0500
Subject: [PATCH] Signup with 3rd party auth can find the matching username
 from the Discourse Hub

---
 .../discourse/controllers/create_account_controller.js   | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/app/assets/javascripts/discourse/controllers/create_account_controller.js b/app/assets/javascripts/discourse/controllers/create_account_controller.js
index 81b1cb521..a136126c8 100644
--- a/app/assets/javascripts/discourse/controllers/create_account_controller.js
+++ b/app/assets/javascripts/discourse/controllers/create_account_controller.js
@@ -98,12 +98,17 @@ Discourse.CreateAccountController = Discourse.Controller.extend(Discourse.ModalF
 
   prefillUsername: function() {
     if (this.get('prefilledUsername')) {
+      // If username field has been filled automatically, and email field just changed,
+      // then remove the username.
       if (this.get('accountUsername') === this.get('prefilledUsername')) {
         this.set('accountUsername', '');
       }
       this.set('prefilledUsername', null);
     }
-    if (this.get('emailValidation.ok') && this.blank('accountUsername')) {
+    if (this.get('emailValidation.ok') && (this.blank('accountUsername') || this.get('authOptions.email'))) {
+      // If email is valid and username has not been entered yet,
+      // or email and username were filled automatically by 3rd parth auth,
+      // then look for a registered username that matches the email.
       this.fetchExistingUsername();
     }
   }.observes('emailValidation', 'accountEmail'),
@@ -111,7 +116,7 @@ Discourse.CreateAccountController = Discourse.Controller.extend(Discourse.ModalF
   fetchExistingUsername: Discourse.debounce(function() {
     var self = this;
     Discourse.User.checkUsername(null, this.get('accountEmail')).then(function(result) {
-      if (result.suggestion && self.blank('accountUsername')) {
+      if (result.suggestion && (self.blank('accountUsername') || self.get('accountUsername') === self.get('authOptions.username'))) {
         self.set('accountUsername', result.suggestion);
         self.set('prefilledUsername', result.suggestion);
       }