Signup form: prefill username if Discourse Hub has a match for the email address. Also, fix some bad specs in username_checker_service_spec that were passing...

This commit is contained in:
Neil Lalonde
2013-11-19 14:15:05 -05:00
parent 309904ef8f
commit 981d8f6aea
8 changed files with 148 additions and 64 deletions

View File

@@ -15,6 +15,7 @@ Discourse.CreateAccountController = Discourse.Controller.extend(Discourse.ModalF
accountChallenge: 0,
formSubmitted: false,
rejectedEmails: Em.A([]),
prefilledUsername: null,
submitDisabled: function() {
if (this.get('formSubmitted')) return true;
@@ -95,6 +96,28 @@ Discourse.CreateAccountController = Discourse.Controller.extend(Discourse.ModalF
});
}.property('accountEmail', 'rejectedEmails.@each'),
prefillUsername: function() {
if (this.get('prefilledUsername')) {
if (this.get('accountUsername') === this.get('prefilledUsername')) {
this.set('accountUsername', '');
}
this.set('prefilledUsername', null);
}
if (this.get('emailValidation.ok') && this.blank('accountUsername')) {
this.fetchExistingUsername();
}
}.observes('emailValidation', 'accountEmail'),
fetchExistingUsername: Discourse.debounce(function() {
var self = this;
Discourse.User.checkUsername(null, this.get('accountEmail')).then(function(result) {
if (result.suggestion && self.blank('accountUsername')) {
self.set('accountUsername', result.suggestion);
self.set('prefilledUsername', result.suggestion);
}
});
}, 500),
usernameMatch: function() {
if (this.usernameNeedsToBeValidatedWithEmail()) {
if (this.get('emailValidation.failed')) {
@@ -119,6 +142,13 @@ Discourse.CreateAccountController = Discourse.Controller.extend(Discourse.ModalF
basicUsernameValidation: function() {
this.set('uniqueUsernameValidation', null);
if (this.get('accountUsername') === this.get('prefilledUsername')) {
return Discourse.InputValidation.create({
ok: true,
reason: I18n.t('user.username.prefilled')
});
}
// If blank, fail without a reason
if (this.blank('accountUsername')) {
return Discourse.InputValidation.create({

View File

@@ -97,7 +97,10 @@ class UsersController < ApplicationController
# Used for checking availability of a username and will return suggestions
# if the username is not available.
def check_username
params.require(:username)
if !params[:username].present?
params.require(:username) if !params[:email].present?
return render(json: success_json) unless SiteSetting.call_discourse_hub?
end
username = params[:username]
target_user = user_from_params_or_current_user
@@ -107,7 +110,7 @@ class UsersController < ApplicationController
checker = UsernameCheckerService.new
email = params[:email] || target_user.try(:email)
render(json: checker.check_username(username, email))
render json: checker.check_username(username, email)
rescue RestClient::Forbidden
render json: {errors: [I18n.t("discourse_hub.access_token_problem")]}
end

View File

@@ -1,15 +1,18 @@
class UsernameCheckerService
def check_username(username, email)
validator = UsernameValidator.new(username)
if !validator.valid_format?
{errors: validator.errors}
elsif !SiteSetting.call_discourse_hub?
check_username_locally(username)
else
check_username_with_hub_server(username, email)
if username && username.length > 0
validator = UsernameValidator.new(username)
if !validator.valid_format?
{errors: validator.errors}
elsif !SiteSetting.call_discourse_hub?
check_username_locally(username)
else
check_username_with_hub_server(username, email)
end
elsif email and SiteSetting.call_discourse_hub?
{suggestion: DiscourseHub.nickname_for_email(email)}
end
end
# Contact the Discourse Hub server