0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-09 21:45:25 +08:00
discourse/app/services/user/action/create_from_verified_email.rb
Keegan George 4522398876
UX: Improve password manager and full name handling in code login (#41746)
**Previously**, the email code login form worked poorly with password
managers (a non-conformant `autocomplete="username email"` hint, and the
email input fully unmounted after the first step so managers lost track
of which account was authenticating), and `full_name_requirement:
required_at_signup` was silently bypassed, with new accounts getting
their generated username as their name.

**In this update**:
- Password managers: the email step uses `autocomplete="username"`, and
a visually-hidden readonly email field preserves the account context on
all later steps.
- Full name requirement: sites requiring full names now collect the name
on the "Almost done" step alongside required user fields, enforced
server-side by a new `required_full_name_provided` policy in
`EmailLoginCode::Redeem`.

Note for API consumers: on sites with `full_name_requirement:
required_at_signup`, `/session/login-code/verify` for a new account now
returns `name_required: true` instead of creating the account until a
`name` is provided.

| Step | Screenshot |
|---|---|
| Email step | ![Email
step](https://github.com/discourse/discourse/releases/download/_gh-attach-assets/01-email-step.png)
|
| Code step | ![Code
step](https://github.com/discourse/discourse/releases/download/_gh-attach-assets/02-code-step.png)
|
| Name required | ![Name required
step](https://github.com/discourse/discourse/releases/download/_gh-attach-assets/03-name-required-step.png)
|
| Empty name error | ![Empty name error
state](https://github.com/discourse/discourse/releases/download/_gh-attach-assets/04-name-error-state.png)
|
| Account ready | ![Account ready
step](https://github.com/discourse/discourse/releases/download/_gh-attach-assets/05-account-ready.png)
|

Feedback:
https://meta.discourse.org/t/easier-account-signup-using-email-codes/407068/10
2026-07-15 10:43:03 -07:00

54 lines
1.3 KiB
Ruby
Vendored

# frozen_string_literal: true
class User::Action::CreateFromVerifiedEmail < Service::ActionBase
option :email
option :ip_address, optional: true
option :user_fields, optional: true
option :name, optional: true
def call
username = UserNameSuggester.suggest(email)
user = User.where(staged: true).with_email(email).first
user&.unstage!
user ||= User.new
user.attributes = {
email: email,
username: username,
name: name.presence || username,
active: false,
locale: I18n.locale,
ip_address: ip_address,
registration_ip_address: ip_address,
}
assign_user_fields(user)
if SiteSetting.must_approve_users? && EmailValidator.can_auto_approve_user?(email)
ReviewableUser.set_approved_fields!(user, Discourse.system_user)
end
user.save!
user
end
private
def assign_user_fields(user)
return if user_fields.blank?
fields = user.custom_fields
UserField
.where(show_on_signup: true)
.pluck(:id)
.each do |field_id|
value = user_fields[field_id.to_s]
value = nil if value == "false"
fields["#{User::USER_FIELD_PREFIX}#{field_id}"] = value[
0...UserField.max_length
] if value.present?
end
user.custom_fields = fields
end
end