From d6c76ceff7d01f5efce979f6968820e74f6c27be Mon Sep 17 00:00:00 2001 From: Linus Gasser Date: Wed, 8 Oct 2025 15:59:56 +0200 Subject: [PATCH] Redirect to password reset in case of existing user --- app/controllers/registrations_controller.rb | 11 +++++++++++ app/views/devise/passwords/new.html.haml | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index 0db33128..3adc60f7 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -3,6 +3,17 @@ class RegistrationsController < Devise::RegistrationsController prepend_before_action :check_captcha, only: [:create] + def create + existing_user = User.find_by(email: sign_up_params[:email]) + if existing_user + flash[:notice] = 'An account with this email already exists. Please reset your password to sign in.' + redirect_to new_user_password_path(user: { email: sign_up_params[:email] }) + return + end + + super + end + protected def after_update_path_for(resource) diff --git a/app/views/devise/passwords/new.html.haml b/app/views/devise/passwords/new.html.haml index 49765d77..31aef5c8 100644 --- a/app/views/devise/passwords/new.html.haml +++ b/app/views/devise/passwords/new.html.haml @@ -10,7 +10,7 @@ .form-group = f.label :email, "Email" %abbr{title: 'This field is required'} * - = f.email_field :email, autofocus: true, required: true, class: 'form-control', placeholder: 'Email' + = f.email_field :email, autofocus: true, required: true, class: 'form-control', placeholder: 'Email', value: params.dig(:user, :email) %p.text-right %button{type: 'submit', class: 'btn btn-success'} Send me reset password instructions