Skip to content

A platform for community discussion. Free, open, simple.

FIX: Leave username blank during OAuth signup when no valid suggestion exists (#36830)

Régis Hanolab298822e26a
diff --git a/frontend/discourse/app/instance-initializers/auth-complete.js b/frontend/discourse/app/instance-initializers/auth-complete.js
index 5fcb4ab80e4..5c57b79a766 100644
--- a/frontend/discourse/app/instance-initializers/auth-complete.js
+++ b/frontend/discourse/app/instance-initializers/auth-complete.js
@@ -101,7 +101,8 @@ export default {
accountUsername: options.username,
accountName: options.name,
authOptions: EmberObject.create(options),
- skipConfirmation: siteSettings.auth_skip_create_confirm,
+ skipConfirmation:
+ siteSettings.auth_skip_create_confirm && options.username,
};
router.transitionTo("signup").then(() => {
diff --git a/frontend/discourse/tests/acceptance/create-account-external-test.js b/frontend/discourse/tests/acceptance/create-account-external-test.js
index 4c81e97023c..c85db41a146 100644
--- a/frontend/discourse/tests/acceptance/create-account-external-test.js
+++ b/frontend/discourse/tests/acceptance/create-account-external-test.js
@@ -6,6 +6,7 @@ function setupAuthData(data) {
data = {
auth_provider: "test",
+ username: "testuser",
can_edit_username: true,
can_edit_name: true,
...data,
@@ -49,6 +50,29 @@ acceptance("Create Account - external auth", function (needs) {
});
});
+acceptance(
+ "Create Account - external auth without username suggestion",
+ function (needs) {
+ needs.hooks.beforeEach(function () {
+ setupAuthData({ username: null });
+ });
+ needs.hooks.afterEach(function () {
+ document.getElementById("data-authentication").remove();
+ });
+
+ test("when skip is enabled but username is missing, shows form", async function (assert) {
+ this.siteSettings.auth_skip_create_confirm = true;
+ await visit("/");
+
+ assert.dom(".signup-fullpage").exists("it shows the signup page");
+
+ assert
+ .dom("#new-account-username")
+ .exists("it shows the fields so user can provide username");
+ });
+ }
+);
+
acceptance("Create account - with associate link", function (needs) {
needs.hooks.beforeEach(function () {
setupAuthData({ associate_url: "/associate/abcde" });
diff --git a/lib/auth/result.rb b/lib/auth/result.rb
index 12e0906d5c1..716e2dd4440 100644
--- a/lib/auth/result.rb
+++ b/lib/auth/result.rb
@@ -203,6 +203,10 @@ class Auth::Result
end
end
- UserNameSuggester.suggest(*username_suggester_attributes, current_username: user&.username)
+ UserNameSuggester.suggest(
+ *username_suggester_attributes,
+ current_username: user&.username,
+ allow_generic_fallback: false,
+ )
end
end
diff --git a/lib/user_name_suggester.rb b/lib/user_name_suggester.rb
index 734bc0f6b91..d7fcd2c7071 100644
--- a/lib/user_name_suggester.rb
+++ b/lib/user_name_suggester.rb
@@ -4,7 +4,7 @@ module UserNameSuggester
GENERIC_NAMES = %w[i me info support admin webmaster hello mail office contact team]
LAST_RESORT_USERNAME = "user"
- def self.suggest(*input, current_username: nil)
+ def self.suggest(*input, current_username: nil, allow_generic_fallback: true)
name =
input.find do |item|
if !SiteSetting.use_email_for_username_and_name_suggestions
@@ -14,7 +14,8 @@ module UserNameSuggester
break parsed_name if sanitize_username(parsed_name).present?
end
- name = fix_username(name)
+ name = fix_username(name, allow_generic_fallback:)
+ return nil if name.blank?
find_available_username_based_on(name, current_username)
end
@@ -101,9 +102,10 @@ module UserNameSuggester
end
end
- def self.fix_username(name)
+ def self.fix_username(name, allow_generic_fallback: true)
fixed_username = sanitize_username(name)
- if fixed_username.empty?
+ if fixed_username.blank?
+ return nil unless allow_generic_fallback
fixed_username << sanitize_username(I18n.t("fallback_username"))
fixed_username << LAST_RESORT_USERNAME if fixed_username.empty?
end
diff --git a/spec/lib/user_name_suggester_spec.rb b/spec/lib/user_name_suggester_spec.rb
index 5daef44f61f..97d2886f8c3 100644
--- a/spec/lib/user_name_suggester_spec.rb
+++ b/spec/lib/user_name_suggester_spec.rb
@@ -168,6 +168,35 @@ RSpec.describe UserNameSuggester do
expect(suggestion).to eq "William"
end
+ context "with allow_generic_fallback: false" do
+ it "returns nil instead of fallback username when input is empty or invalid" do
+ expect(UserNameSuggester.suggest(nil, allow_generic_fallback: false)).to be_nil
+ expect(UserNameSuggester.suggest("", allow_generic_fallback: false)).to be_nil
+ expect(UserNameSuggester.suggest("---", allow_generic_fallback: false)).to be_nil
+ end
+
+ it "still suggests valid usernames" do
+ expect(UserNameSuggester.suggest("john", allow_generic_fallback: false)).to eq("john")
+ end
+
+ it "still increments valid usernames" do
+ Fabricate(:user, username: "jane")
+ expect(UserNameSuggester.suggest("jane", allow_generic_fallback: false)).to eq("jane1")
+ end
+
+ it "respects use_email_for_username_and_name_suggestions setting" do
+ SiteSetting.use_email_for_username_and_name_suggestions = false
+ expect(
+ UserNameSuggester.suggest("[email protected]", allow_generic_fallback: false),
+ ).to be_nil
+
+ SiteSetting.use_email_for_username_and_name_suggestions = true
+ expect(UserNameSuggester.suggest("[email protected]", allow_generic_fallback: false)).to eq(
+ "bob",
+ )
+ end
+ end
+
context "with Unicode usernames disabled" do
before { SiteSetting.unicode_usernames = false }
diff --git a/spec/requests/omniauth_callbacks_controller_spec.rb b/spec/requests/omniauth_callbacks_controller_spec.rb
index 96187c38c77..20db4c3e3ab 100644
--- a/spec/requests/omniauth_callbacks_controller_spec.rb
+++ b/spec/requests/omniauth_callbacks_controller_spec.rb
@@ -372,7 +372,8 @@ RSpec.describe Users::OmniauthCallbacksController do
get "/auth/google_oauth2/callback.json"
data = JSON.parse(cookies[:authentication_data])
- expect(data["username"]).to eq("user1") # not "billmailbox" that can be extracted from email
+ # leaves field blank for user to choose
+ expect(data["username"]).to eq(nil)
end
it "uses email for username suggestions if enabled in settings" do
@@ -398,7 +399,8 @@ RSpec.describe Users::OmniauthCallbacksController do
get "/auth/google_oauth2/callback.json"
data = JSON.parse(cookies[:authentication_data])
- expect(data["username"]).to eq("user1")
+ # leaves field blank for user to choose
+ expect(data["username"]).to eq(nil)
end
describe "when site is invite_only" do
diff --git a/spec/system/discourse_id_spec.rb b/spec/system/discourse_id_spec.rb
index e5296d9e8a7..8a2ab4c4570 100644
--- a/spec/system/discourse_id_spec.rb
+++ b/spec/system/discourse_id_spec.rb
@@ -15,7 +15,7 @@ describe "discourse login client auth" do
info:
OmniAuth::AuthHash::InfoHash.new(
email: OmniauthHelpers::EMAIL,
- username: OmniauthHelpers::USERNAME,
+ nickname: OmniauthHelpers::USERNAME,
),
)
end
diff --git a/spec/system/social_authentication_spec.rb b/spec/system/social_authentication_spec.rb
index c1508ab99b1..5a45859f096 100644
--- a/spec/system/social_authentication_spec.rb
+++ b/spec/system/social_authentication_spec.rb
@@ -282,6 +282,17 @@ shared_examples "social authentication scenarios" do
signup_page.open.click_social_button("google_oauth2")
expect(page).to have_css(".header-dropdown-toggle.current-user")
end
+
+ it "shows signup form when no username can be derived" do
+ SiteSetting.use_email_for_username_and_name_suggestions = false
+ mock_google_auth(name: "")
+ visit("/")
+
+ signup_page.open.click_social_button("google_oauth2")
+ expect(signup_page).to be_open
+ expect(signup_page).to have_no_password_input
+ expect(page).to have_field("new-account-username", with: "")
+ end
end
context "when there is only one external login method enabled" do