Converting existing users to use Google Auth with a secondary email

AI-generated summary

ian937 is running a private Discourse instance with ~200 users currently authenticated via personal emails. They wish to migrate to Google SSO using specific organization Google accounts, forcing users to authenticate only via these domain accounts while retaining their personal emails for notifications. To avoid manual user intervention, ian937 proposed a plan to programmatically add Google emails as secondary addresses via a Ruby script, configure OAuth2, and restrict authentication to the organization’s domain.

olivia confirmed the general approach, noting that Discourse matches SSO logins to existing accounts via secondary emails, allowing users to keep personal emails as primary for notifications. However, she provided several critical corrections to ian937’s implementation:

  1. Domain Restriction: Instead of using Allowed email domains, she advised using the google oauth2 hd setting to restrict logins to the specific Google Workspace domain.
  2. Email Settings: The auth overrides email setting should remain disabled to ensure primary email addresses (personal ones) are not overwritten.
  3. Script Fixes: The provided Ruby script contained errors. UserEmail does not have a confirmed_at column, and the comment line lacked the # prefix. She suggested a corrected creation step: user.user_emails.create!(email: domain_email, primary: false).

Olivia strongly recommended testing the script on a backup with a few users first, as it bypasses email verification. She also advised verifying that Google login correctly maps to existing accounts and that private Google accounts are blocked before eventually disabling local logins.

We run a private Discourse site for our association with a little over 200 active users. Currently the vast majority are signed up with their personal email accounts rather than the Google account we create for them (because the Google accounts didn’t exist when we first setup Discourse, and a lot of people are more likely to check their personal email for notifications anyway).
Now I want to switch to using the Google accounts as the authentication source, but I want to try and avoid having to get users to manually change or add their google account emails themselves (because trying to get non-technical volunteers to do something like that always turns into a nightmare for me). I also want to avoid users with private google accounts using those for SSO, and force all users to SSO to our Google domain accounts.

Having done some Googling, I’ve come up with a possible plan to achieve this, but want to try and confirm that I’m on the right track. Here’s the plan:

  1. Add all the Google email addresses as secondary emails (I’ve already enabled Secondary email addresses in the settings)
  2. Setup an OAuth2 consent and credentials, as per the Discourse docs
  3. Enter the credentials in Admin > Login & Authentication > OAuth2, google oauth2 client id & google oauth2 client secret. Then enable the setting enable google oauth2 logins.
  4. Restrict authentication to our domain by setting Admin > Login & Authentication, Allowed email domains - is this necessary?

Also, Google’s AI suggested using this Ruby script to automate adding the email addresses:

require 'csv'

csv_path = "/shared/users_map.csv"
updated = 0
skipped = 0

CSV.foreach(csv_path) do |personal_email, domain_email|
personal_email = personal_email&.strip&.downcase
domain_email = domain_email&.strip&.downcase
next if personal_email.blank? || domain_email.blank?

user = User.find_by_email(personal_email)

if user.nil?
puts "NOT FOUND: #{personal_email}"
skipped += 1
next
end

if UserEmail.exists?(email: domain_email)
puts "ALREADY EXISTS: #{domain_email} (User: #{user.username})"
skipped += 1
next
end

Attach the domain email as a confirmed secondary email

UserEmail.create!(
user_id: user.id,
email: domain_email,
primary: false,
confirmed_at: Time.zone.now
)

puts "LINKED: #{user.username} -> #{domain_email}"
updated += 1
end

puts "\nDone! Updated: #{updated}, Skipped/Errors: #{skipped}"

Any feedback appreciated!

Your plan looks right. Discourse can match Google logins to existing accounts through secondary emails, so users can keep their personal emails for notifications.

For the domain restriction, use google oauth2 hd rather than allowed email domains. Also, leave auth overrides email disabled to keep their primary email addresses unchanged.

For the script, UserEmail doesn’t have a confirmed_at column, so remove that. The Attach the domain email... line also needs a # in front of it.

The create step can be:

user.user_emails.create!(email: domain_email, primary: false)

I’d take a backup and test with a couple of users first, especially since the script skips email verification. Check that Google login takes them to their existing accounts and that personal Google accounts are blocked. Once that’s working, you can disable local logins.