identity-vault/app/controllers/identity_backup_codes_controller.rb
nora 87bb6d4a18
Switch mailers to SES (#72)
* add premailer

* first pass at converting existing mailers

* this EIN is not yours :-P

* remove some css that probably won't work

* that was gonna bug me

* more mailers!

* s/account/auth

* rework 2fa/security mailers

* env vars for SES creds

* add OpenSSL explicitly

* use external logo image

* nuke step_up_code
2025-12-04 16:32:32 -05:00

41 lines
1.4 KiB
Ruby

class IdentityBackupCodesController < ApplicationController
def index
@backup_codes = current_identity.backup_codes.active.order(created_at: :desc)
render layout: request.headers["HX-Request"] ? "htmx" : false
end
def create
# Generate new backup codes in previewed state
codes_to_save = []
10.times do
backup_code = SecureRandom.alphanumeric(10).upcase
codes_to_save << backup_code
current_identity.backup_codes.create!(code: backup_code, aasm_state: :previewed)
end
@backup_codes = current_identity.backup_codes.active.order(created_at: :desc)
@newly_generated_codes = codes_to_save
if request.headers["HX-Request"]
render :index, layout: "htmx"
else
render :index, notice: "New backup codes generated. Save them now - you won't see them again!"
end
end
def confirm
current_identity.backup_codes.active.each(&:mark_discarded!)
current_identity.backup_codes.previewed.each(&:mark_active!)
current_identity.create_activity :regenerate_backup_codes, owner: current_identity, recipient: current_identity
IdentityBackupCodeMailer.codes_regenerated(current_identity).deliver_later
@backup_codes = current_identity.backup_codes.active.order(created_at: :desc)
if request.headers["HX-Request"]
render :index, layout: "htmx"
else
redirect_to identity_backup_codes_path, notice: "Backup codes updated successfully"
end
end
end