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:
- Add all the Google email addresses as secondary emails (I’ve already enabled Secondary email addresses in the settings)
- Setup an OAuth2 consent and credentials, as per the Discourse docs
- Enter the credentials in Admin > Login & Authentication > OAuth2, google oauth2 client id & google oauth2 client secret. Then enable the setting enable google oauth2 logins.
- 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!