From f2cd0dae96e176adf7536df650a853459e74f60b Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Tue, 17 May 2022 13:20:31 +0100 Subject: [PATCH] Redirect confirmation to set password --- .../auth/confirmations_controller.rb | 24 +++++++++++++++++++ .../devise/passwords/reset_password.html.erb | 2 +- config/locales/devise.en.yml | 8 +++---- config/routes.rb | 2 +- 4 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 app/controllers/auth/confirmations_controller.rb diff --git a/app/controllers/auth/confirmations_controller.rb b/app/controllers/auth/confirmations_controller.rb new file mode 100644 index 000000000..9da8a5689 --- /dev/null +++ b/app/controllers/auth/confirmations_controller.rb @@ -0,0 +1,24 @@ +class Auth::ConfirmationsController < Devise::ConfirmationsController + + # GET /resource/confirmation?confirmation_token=abcdef + def show + self.resource = resource_class.confirm_by_token(params[:confirmation_token]) + yield resource if block_given? + + if resource.errors.empty? + set_flash_message!(:notice, :confirmed) + token = resource.send(:set_reset_password_token) + base = public_send("edit_#{resource_class.name.underscore}_password_url") + url = "#{base}?reset_password_token=#{token}" + redirect_to url + else + respond_with_navigational(resource.errors, status: :unprocessable_entity){ render :new } + end + end + +protected + + def after_confirmation_path_for(resource_name, resource) + edit_user_password_path(email: resource.email) + end +end diff --git a/app/views/devise/passwords/reset_password.html.erb b/app/views/devise/passwords/reset_password.html.erb index 04353c7b2..f66ca4240 100644 --- a/app/views/devise/passwords/reset_password.html.erb +++ b/app/views/devise/passwords/reset_password.html.erb @@ -1,4 +1,4 @@ -<% content_for :title, "Reset your password" %> +<% content_for :title, resource.sign_in_count.zero? ? "Set your password" : "Reset your password" %> <% content_for :before_content do %> <%= govuk_back_link( diff --git a/config/locales/devise.en.yml b/config/locales/devise.en.yml index a0a6ad4ff..8fa8d8ffa 100644 --- a/config/locales/devise.en.yml +++ b/config/locales/devise.en.yml @@ -56,10 +56,10 @@ en: unlocked: "Your account has been successfully unlocked. Sign in to continue." errors: messages: - already_confirmed: "has already been confirmed. Sign in." - blank: "can’t be blank" - confirmation: "doesn’t match new password" - confirmation_period_expired: "needs to be confirmed within %{period}. Request a new account." + already_confirmed: "Email has already been confirmed. Sign in." + blank: "Email can’t be blank" + confirmation: "Passwords don’t match" + confirmation_period_expired: "Email needs to be confirmed within %{period}. Request a new account." expired: "Token has expired. Request a new token." not_found: "was not found" not_locked: "has not been locked" diff --git a/config/routes.rb b/config/routes.rb index 4d5f94205..f8269ed12 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -26,7 +26,7 @@ Rails.application.routes.draw do devise_for :users, { path: :account, controllers: { - confirmations: "devise/confirmations", + confirmations: "auth/confirmations", passwords: "auth/passwords", sessions: "auth/sessions", two_factor_authentication: "auth/two_factor_authentication",