A platform for community discussion. Free, open, simple.
FIX: Suggest current username for staged users (#13706)
Dan Ungureanu49090c3524b3
diff --git a/lib/auth/result.rb b/lib/auth/result.rbindex 35ce4cab095..1b9c1d85b0b 100644--- a/lib/auth/result.rb+++ b/lib/auth/result.rb@@ -137,9 +137,18 @@ class Auth::Resultreturn resultend+ suggested_username = UserNameSuggester.suggest(username_suggester_attributes)+ if email_valid && email.present?+ if username.present? && User.username_available?(username, email)+ suggested_username = username+ elsif staged_user = User.where(staged: true).find_by_email(email)+ suggested_username = staged_user.username+ end+ end+result = {email: email,- username: UserNameSuggester.suggest(username_suggester_attributes),+ username: suggested_username,auth_provider: authenticator_name,email_valid: !!email_valid,can_edit_username: can_edit_username,diff --git a/spec/requests/omniauth_callbacks_controller_spec.rb b/spec/requests/omniauth_callbacks_controller_spec.rbindex 8f99c8ec33e..331678dc5d5 100644--- a/spec/requests/omniauth_callbacks_controller_spec.rb+++ b/spec/requests/omniauth_callbacks_controller_spec.rb@@ -213,6 +213,29 @@ RSpec.describe Users::OmniauthCallbacksController doexpect(data["destination_url"]).to eq(destination_url)end+ it 'should return the right response for staged users' do+ Fabricate(:user, username: "Staged_User", email: email, staged: true)++ destination_url = '/somepath'+ Rails.application.env_config["omniauth.origin"] = destination_url++ events = DiscourseEvent.track_events { get "/auth/google_oauth2/callback.json" }+ expect(events.any? { |e| e[:event_name] == :before_auth }).to eq(true)+ expect(events.any? { |e| e[:event_name] === :after_auth && Auth::GoogleOAuth2Authenticator === e[:params][0] && !e[:params][1].failed? }).to eq(true)++ expect(response.status).to eq(302)++ data = JSON.parse(cookies[:authentication_data])++ expect(data["email"]).to eq(email)+ expect(data["username"]).to eq("Staged_User")+ expect(data["auth_provider"]).to eq("google_oauth2")+ expect(data["email_valid"]).to eq(true)+ expect(data["can_edit_username"]).to eq(true)+ expect(data["name"]).to eq("Some Name")+ expect(data["destination_url"]).to eq(destination_url)+ end+it 'should include destination url in response' dodestination_url = '/cookiepath'cookies[:destination_url] = destination_url