| 1 | # frozen_string_literal: true |
| 2 | |
| 3 | class ConfirmationsController < ApplicationController |
| 4 | before_action :noindex! |
| 5 | before_action :redirect_if_signed_in |
| 6 | |
| 7 | # GET /auth/confirmation/resend |
| 8 | def new |
| 9 | end |
| 10 | |
| 11 | # GET /auth/confirmed |
| 12 | # Landing page for the link in the confirmation email. smbCloud verifies the |
| 13 | # token on its side, then redirects here with ?status=success or |
| 14 | # ?status=failed&message=... (see redirect_or_render_confirmation in the |
| 15 | # smbCloud client auth_users controller). |
| 16 | def confirmed |
| 17 | @succeeded = params[:status].to_s == "success" |
| 18 | @message = params[:message].to_s.strip.presence |
| 19 | |
| 20 | # "Already confirmed" is a benign state, not a real failure: the account is |
| 21 | # fine and the user just needs to sign in. smbCloud reports it as |
| 22 | # status=failed with this message, so detect it and treat it gently rather |
| 23 | # than showing the scary failure UI with a pointless "resend" button. |
| 24 | @already_confirmed = !@succeeded && @message.to_s.match?(/already confirmed/i) |
| 25 | end |
| 26 | |
| 27 | # POST /auth/confirmation/resend |
| 28 | def create |
| 29 | email = params[:email].to_s.strip.downcase |
| 30 | |
| 31 | if email.blank? |
| 32 | flash.now[:alert] = "Email is required." |
| 33 | return render :new, status: :unprocessable_entity |
| 34 | end |
| 35 | |
| 36 | SmbcloudAuthService.resend_confirmation(email: email) |
| 37 | |
| 38 | # The endpoint never reveals whether the account exists or is already |
| 39 | # confirmed, so we always show the same neutral message. |
| 40 | redirect_to signin_path, |
| 41 | notice: "If that email has an unconfirmed account, we've sent a new confirmation link. Please check your inbox." |
| 42 | |
| 43 | rescue KeyError => e |
| 44 | Rails.logger.error("smbCloud configuration error during resend confirmation: #{e.message}") |
| 45 | flash.now[:alert] = "Authentication service is not configured. Please contact support." |
| 46 | render :new, status: :internal_server_error |
| 47 | |
| 48 | rescue SmbcloudAuthService::AuthenticationError => e |
| 49 | Rails.logger.warn("resend confirmation failed for #{email.inspect}: #{e.message}") |
| 50 | flash.now[:alert] = "Something went wrong sending the confirmation email. Please try again." |
| 51 | render :new, status: :internal_server_error |
| 52 | end |
| 53 | |
| 54 | private |
| 55 | |
| 56 | def redirect_if_signed_in |
| 57 | redirect_to root_path, notice: "You are already signed in." if signed_in? |
| 58 | end |
| 59 | end |