mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
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:
@@ -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({
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user