Model FC : au lieu de valider le mail on envoie une demande de confirmation par mail

This commit is contained in:
Kara Diaby 2024-07-01 14:41:02 +00:00
parent f531ba65f2
commit 9c41c9a08f
No known key found for this signature in database
GPG key ID: C4D1B0CF9F24D759

View file

@ -2,6 +2,7 @@
class FranceConnectInformation < ApplicationRecord class FranceConnectInformation < ApplicationRecord
MERGE_VALIDITY = 15.minutes MERGE_VALIDITY = 15.minutes
CONFIRMATION_EMAIL_VALIDITY = 2.days
belongs_to :user, optional: true belongs_to :user, optional: true
@ -14,9 +15,9 @@ class FranceConnectInformation < ApplicationRecord
password: Devise.friendly_token[0, 20], password: Devise.friendly_token[0, 20],
confirmed_at: Time.zone.now confirmed_at: Time.zone.now
) )
user.after_confirmation send_custom_confirmation_instructions(user)
rescue ActiveRecord::RecordNotUnique rescue ActiveRecord::RecordNotUnique
# ignore this exception because we check before is user is nil. # ignore this exception because we check before if user is nil.
# exception can be raised in race conditions, when FranceConnect calls callback 2 times. # 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 # 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 # because the first call has already created a user
@ -26,6 +27,12 @@ class FranceConnectInformation < ApplicationRecord
touch # needed to update updated_at column touch # needed to update updated_at column
end end
def send_custom_confirmation_instructions(user)
token = SecureRandom.hex(10)
user.update!(confirmation_token: token, confirmation_sent_at: Time.zone.now)
UserMailer.custom_confirmation_instructions(user, token).deliver_later
end
def create_merge_token! def create_merge_token!
merge_token = SecureRandom.uuid merge_token = SecureRandom.uuid
update(merge_token:, merge_token_created_at: Time.zone.now) update(merge_token:, merge_token_created_at: Time.zone.now)