chore: Refactor DeviseOverrides::ConfirmationsController#create (#1297)

This commit is contained in:
Dmitriy Shcherbakan
2020-10-02 12:57:26 +03:00
committed by GitHub
parent b81eef6ffd
commit 98cb09104e
2 changed files with 78 additions and 10 deletions

View File

@@ -4,22 +4,34 @@ class DeviseOverrides::ConfirmationsController < Devise::ConfirmationsController
def create
@confirmable = User.find_by(confirmation_token: params[:confirmation_token])
if @confirmable
if @confirmable.confirm || (@confirmable.confirmed_at && @confirmable.reset_password_token)
# confirmed now or already confirmed but quit before setting a password
render json: { "message": 'Success', "redirect_url": create_reset_token_link(@confirmable) }, status: :ok
elsif @confirmable.confirmed_at
render json: { "message": 'Already confirmed', "redirect_url": '/' }, status: 422
else
render json: { "message": 'Failure', "redirect_url": '/' }, status: 422
end
if confirm
render_confirmation_success
else
render json: { "message": 'Invalid token', "redirect_url": '/' }, status: 422
render_confirmation_error
end
end
protected
def confirm
@confirmable&.confirm || (@confirmable&.confirmed_at && @confirmable&.reset_password_token)
end
def render_confirmation_success
render json: { "message": 'Success', "redirect_url": create_reset_token_link(@confirmable) }, status: :ok
end
def render_confirmation_error
if @confirmable.blank?
render json: { "message": 'Invalid token', "redirect_url": '/' }, status: 422
elsif @confirmable.confirmed_at
render json: { "message": 'Already confirmed', "redirect_url": '/' }, status: 422
else
render json: { "message": 'Failure', "redirect_url": '/' }, status: 422
end
end
def create_reset_token_link(user)
raw, enc = Devise.token_generator.generate(user.class, :reset_password_token)
user.reset_password_token = enc