ignore when FranceConnect calls callback 2 times

This commit is contained in:
Christophe Robillard 2021-02-09 14:35:54 +01:00
parent a288a13805
commit 966ee33529

View file

@ -23,11 +23,18 @@ class FranceConnectInformation < ApplicationRecord
user = User.find_by(email: email_france_connect.downcase) user = User.find_by(email: email_france_connect.downcase)
if user.nil? if user.nil?
user = User.create!( begin
email: email_france_connect.downcase, user = User.create!(
password: Devise.friendly_token[0, 20], email: email_france_connect.downcase,
confirmed_at: Time.zone.now password: Devise.friendly_token[0, 20],
) confirmed_at: Time.zone.now
)
rescue ActiveRecord::RecordNotUnique
# ignore this exception because we check before is user is nil.
# exception can be raised in race conditions, when FranceConnect calls callback 2 times.
# At the 2nd call, user is nil but exception is raised at the creation of the user
# because the first call has already created a user
end
end end
update_attribute('user_id', user.id) update_attribute('user_id', user.id)