1
0

confirmations_controller.rb 1.2 KB

123456789101112131415161718192021222324252627282930
  1. # frozen_string_literal: true
  2. class Api::V1::Emails::ConfirmationsController < Api::BaseController
  3. before_action -> { authorize_if_got_token! :read, :'read:accounts' }, only: :check
  4. before_action -> { doorkeeper_authorize! :write, :'write:accounts' }, except: :check
  5. before_action :require_user_owned_by_application!, except: :check
  6. before_action :require_user_not_confirmed!, except: :check
  7. before_action :require_authenticated_user!, only: :check
  8. def create
  9. current_user.update!(email: params[:email]) if params.key?(:email)
  10. current_user.resend_confirmation_instructions
  11. render_empty
  12. end
  13. def check
  14. render json: current_user.confirmed?
  15. end
  16. private
  17. def require_user_owned_by_application!
  18. render json: { error: 'This method is only available to the application the user originally signed-up with' }, status: 403 unless current_user && current_user.created_by_application_id == doorkeeper_token.application_id
  19. end
  20. def require_user_not_confirmed!
  21. render json: { error: 'This method is only available while the e-mail is awaiting confirmation' }, status: 403 unless !current_user.confirmed? || current_user.unconfirmed_email.present?
  22. end
  23. end