Merge pull request #12997 from wandji20/wb-OFN-12794-user

Remove Cable Ready responses from User related controllers
This commit is contained in:
Rachel Arnould
2026-02-03 15:51:11 +01:00
committed by GitHub
13 changed files with 63 additions and 120 deletions

View File

@@ -9,7 +9,6 @@ module Spree
include Spree::Core::ControllerHelpers::Auth
include Spree::Core::ControllerHelpers::Common
include Spree::Core::ControllerHelpers::Order
include CablecarResponses
helper 'spree/base'
@@ -24,14 +23,12 @@ module Spree
if spree_user_signed_in?
flash[:success] = t('devise.success.logged_in_succesfully')
render cable_ready: cable_car.redirect_to(
url: return_url_or_default(after_sign_in_path_for(spree_current_user))
)
redirect_to return_url_or_default(after_sign_in_path_for(spree_current_user))
else
render status: :unauthorized, cable_ready: cable_car.inner_html(
"#login-feedback",
partial("layouts/alert", locals: { type: "alert", message: t('devise.failure.invalid') })
)
message = t('devise.failure.invalid')
render turbo_stream: turbo_stream.update(
'login-feedback', partial: 'layouts/alert', locals: { message:, type: 'alert' }
), status: :unprocessable_entity
end
end
@@ -60,11 +57,13 @@ module Spree
end
def render_unconfirmed_response
render status: :unprocessable_entity, cable_ready: cable_car.inner_html(
"#login-feedback",
partial("layouts/alert", locals: { type: "alert", message: t(:email_unconfirmed),
unconfirmed: true, tab: "login" })
)
message = t(:email_unconfirmed)
render turbo_stream: turbo_stream.update(
'login-feedback',
partial: 'layouts/alert', locals: { type: "alert", message:, unconfirmed: true,
tab: "login", email: params.dig(:spree_user, :email) }
), status: :unprocessable_entity
end
def ensure_valid_locale_persisted

View File

@@ -3,7 +3,6 @@
module Spree
class UsersController < ::BaseController
include I18nHelper
include CablecarResponses
layout 'darkswarm'
@@ -25,34 +24,17 @@ module Spree
@unconfirmed_email = spree_current_user.unconfirmed_email
end
# Endpoint for queries to check if a user is already registered
def registered_email
registered = Spree::User.find_by(email: params[:email]).present?
if registered
render status: :ok, cable_ready: cable_car.
inner_html(
"#login-feedback",
partial("layouts/alert",
locals: { type: "alert", message: t('devise.failure.already_registered') })
).
dispatch_event(name: "login:modal:open")
else
head :not_found
end
end
def create
@user = Spree::User.new(user_params)
if @user.save
flash[:success] = t('devise.user_registrations.spree_user.signed_up_but_unconfirmed')
render cable_ready: cable_car.redirect_to(url: main_app.root_path)
redirect_to main_app.root_path
else
render status: :unprocessable_entity, cable_ready: cable_car.morph(
"#signup-tab",
partial("layouts/signup_tab", locals: { signup_form_user: @user })
)
render turbo_stream: turbo_stream.update(
'signup-tab',
partial: 'layouts/signup_tab', locals: { signup_form_user: @user }
), status: :unprocessable_entity
end
end
@@ -97,13 +79,10 @@ module Spree
end
def render_alert_timestamp_error_message
render cable_ready: cable_car.inner_html(
"#signup-feedback",
partial("layouts/alert",
locals: {
type: "alert",
message: InvisibleCaptcha.timestamp_error_message
})
render turbo_stream: turbo_stream.update(
'signup-feedback',
partial: 'layouts/alert',
locals: { type: "alert", message: InvisibleCaptcha.timestamp_error_message }
)
end
end

View File

@@ -3,7 +3,6 @@
class UserConfirmationsController < DeviseController
# Needed for access to current_ability, so we can authorize! actions
include Spree::Core::ControllerHelpers::Auth
include CablecarResponses
# GET /resource/confirmation?confirmation_token=abcdef
def show
@@ -29,12 +28,12 @@ class UserConfirmationsController < DeviseController
set_flash_message(:error, :confirmation_not_sent)
end
else
render cable_ready: cable_car.inner_html(
"##{params[:tab] || 'forgot'}-feedback",
partial("layouts/alert",
locals: { type: "success", message: t("devise.confirmations.send_instructions") })
flash.now[:sucess] = t("devise.confirmations.send_instructions")
return render turbo_stream: turbo_stream.update(
"#{params[:tab] || 'forgot'}-feedback",
partial: 'shared/flashes', locals: { flashes: flash }
)
return
end
respond_with_navigational(resource){ redirect_to login_path }

View File

@@ -1,37 +1,40 @@
# frozen_string_literal: true
class UserPasswordsController < Spree::UserPasswordsController
include CablecarResponses
layout 'darkswarm'
def create
return render_unconfirmed_response if user_unconfirmed?
self.resource = resource_class.send_reset_password_instructions(raw_params[resource_name])
status = :ok
if resource.errors.empty?
render cable_ready: cable_car.inner_html(
"#forgot-feedback",
partial("layouts/alert", locals: { type: "success", message: t(:password_reset_sent) })
)
message, type = [t(:password_reset_sent), :success]
else
render status: :not_found, cable_ready: cable_car.inner_html(
"#forgot-feedback",
partial("layouts/alert", locals: { type: "alert", message: t(:email_not_found) })
)
message, type = [t(:email_not_found), :alert]
status = :not_found
end
render turbo_stream: turbo_stream.update(
'forgot-feedback',
partial: 'layouts/alert',
locals: { type:, message:, tab: 'forgot',
unconfirmed: false, email: params.dig(:spree_user, :email) }
), status:
end
private
def render_unconfirmed_response
render status: :unprocessable_entity, cable_ready: cable_car.inner_html(
"#forgot-feedback",
partial("layouts/alert",
locals: { type: "alert", message: t(:email_unconfirmed),
unconfirmed: true, tab: "forgot" })
)
message, type, unconfirmed, tab = [t(:email_unconfirmed), :alert, true, 'forgot']
render turbo_stream: turbo_stream.update(
'forgot-feedback',
partial: 'layouts/alert',
locals: { type:, message:, tab:,
unconfirmed:, email: params.dig(:spree_user, :email) }
), status: :unprocessable_entity
end
def user_unconfirmed?

View File

@@ -1,5 +1,5 @@
.alert-box{ class: "#{type}" }
= message
- if local_assigns[:unconfirmed]
%a{ "data-action": "login-modal#resend_confirmation", "data-tab": local_assigns[:tab] }
= link_to spree_user_confirmation_path(spree_user: { email: }, tab: local_assigns[:tab]), data: { turbo_method: :post } do
= t('devise.confirmations.resend_confirmation_email')

View File

@@ -1,12 +1,12 @@
#forgot-tab
= form_with url: spree_user_password_path, scope: :spree_user, data: { remote: "true" } do |form|
= form_with url: spree_user_password_path, scope: :spree_user, data: { turbo: true } do |form|
.row
.large-12.columns#forgot-feedback
.row
.large-12.columns
= form.label :email, t(:signup_email)
= form.email_field :email, { tabindex: 1, inputmode: "email", "data-login-modal-target": "email", "data-action": "input->login-modal#emailOnInput" }
= form.email_field :email, { tabindex: 1, inputmode: "email" }
.row
.large-12.columns
= form.submit t(:reset_password), { class: "button primary", tabindex: 2 }

View File

@@ -1,5 +1,5 @@
#login-content
= form_with url: spree_user_session_path, scope: :spree_user, data: { remote: "true" } do |form|
= form_with url: spree_user_session_path, scope: :spree_user, data: { turbo: true } do |form|
.row
.large-12.columns#login-feedback
- confirmation_result = request.query_parameters[:validation]
@@ -10,7 +10,7 @@
.row
.large-12.columns
= form.label :email, t(:email)
= form.email_field :email, { tabindex: 1, inputmode: "email", autocomplete: "off", "data-login-modal-target": "email", "data-action": "input->login-modal#emailOnInput" }
= form.email_field :email, { tabindex: 1, inputmode: "email", autocomplete: "off" }
.row
.large-12.columns
= form.label :password, t(:password)

View File

@@ -1,14 +1,14 @@
- signup_form_user = Spree::User.new if local_assigns[:signup_form_user].nil?
#signup-tab
= form_with model: signup_form_user, url: spree.account_path, scope: :user, data: { remote: "true" } do |form|
= form_with model: signup_form_user, url: spree.account_path, scope: :user, data: { turbo: true } do |form|
.row
.large-12.columns#signup-feedback
.row
.large-12.columns
= form.label :email, t(:signup_email)
= form.email_field :email, { tabindex: 1, "data-login-modal-target": "email", "data-action": "input->login-modal#emailOnInput" }
= form.email_field :email, { tabindex: 1 }
= form.error_message_on :email
.row
.large-12.columns

View File

@@ -1,8 +1,7 @@
import { Controller } from "stimulus";
export default class extends Controller {
static targets = ["background", "modal", "email"];
static values = { email: String };
static targets = ["background", "modal"];
connect() {
if (this.hasModalTarget) {
@@ -19,13 +18,6 @@ export default class extends Controller {
window.dispatchEvent(new Event("login:modal:open"));
}
emailOnInput(event) {
this.emailValue = event.currentTarget.value;
this.emailTargets.forEach((element) => {
element.value = this.emailValue;
});
}
open = () => {
if (!location.hash.substr(1).includes("/login")) {
history.pushState({}, "", "#/login");
@@ -57,19 +49,6 @@ export default class extends Controller {
}, 200);
}
resend_confirmation(event) {
fetch("/user/spree_user/confirmation", {
method: "POST",
body: JSON.stringify({
spree_user: { email: this.emailValue },
tab: event.currentTarget.dataset.tab,
}),
headers: { "Content-type": "application/json; charset=UTF-8" },
})
.then((data) => data.json())
.then(CableReady.perform);
}
returnHome() {
window.location = "/";
}

View File

@@ -16,7 +16,6 @@ Openfoodnetwork::Application.routes.draw do
get "/register", to: "registration#index", as: :registration
get "/register/auth", to: "registration#authenticate", as: :registration_auth
post "/user/registered_email", to: "spree/users#registered_email"
resources :locales, only: [:show]
# Redirects to global website

View File

@@ -13,8 +13,8 @@ RSpec.describe Spree::UserSessionsController do
it "redirects to root" do
spree_post :create, spree_user: { email: user.email, password: user.password }
expect(response).to have_http_status(:ok)
expect(response.body).to match(root_path).and match("redirect")
expect(response).to have_http_status(:found)
expect(response.location).to eq root_url
end
end
@@ -24,8 +24,8 @@ RSpec.describe Spree::UserSessionsController do
it "redirects to checkout" do
spree_post :create, spree_user: { email: user.email, password: user.password }
expect(response).to have_http_status(:ok)
expect(response.body).to match(checkout_path).and match("redirect")
expect(response).to have_http_status(:found)
expect(response.location).to eq checkout_url
end
end
end
@@ -34,9 +34,10 @@ RSpec.describe Spree::UserSessionsController do
render_views
it "returns an error" do
spree_post :create, spree_user: { email: user.email, password: "wrong" }
spree_post :create, spree_user: { email: user.email, password: "wrong" },
format: :turbo_stream
expect(response).to have_http_status(:unauthorized)
expect(response).to have_http_status(:unprocessable_entity)
expect(response.body).to include "Invalid email or password"
end
end

View File

@@ -53,22 +53,6 @@ RSpec.describe Spree::UsersController do
end
end
describe "#registered_email" do
routes { Openfoodnetwork::Application.routes }
let!(:user) { create(:user) }
it "returns ok (200) if email corresponds to a registered user" do
post :registered_email, params: { email: user.email }
expect(response).to have_http_status(:ok)
end
it "returns not_found (404) if email does not correspond to a registered user" do
post :registered_email, params: { email: 'nonregistereduser@example.com' }
expect(response).to have_http_status(:not_found)
end
end
describe '#load_object' do
it 'redirects to signup path if user is not found' do
allow(controller).to receive_messages(spree_current_user: nil)

View File

@@ -12,20 +12,20 @@ RSpec.describe UserPasswordsController do
describe "create" do
it "returns 404 if user is not found" do
spree_post :create, spree_user: { email: "xxxxxxxxxx@example.com" }
spree_post :create, spree_user: { email: "xxxxxxxxxx@example.com" }, format: :turbo_stream
expect(response).to have_http_status :not_found
expect(response.body).to match 'Email address not found'
end
it "returns 422 if user is registered but not confirmed" do
spree_post :create, spree_user: { email: unconfirmed_user.email }
spree_post :create, spree_user: { email: unconfirmed_user.email }, format: :turbo_stream
expect(response).to have_http_status :unprocessable_entity
expect(response.body).to match "You must confirm your email \
address before you can reset your password."
end
it "returns 200 when password reset was successful" do
spree_post :create, spree_user: { email: user.email }
spree_post :create, spree_user: { email: user.email }, format: :turbo_stream
expect(response).to have_http_status :ok
expect(response.body).to match "An email with instructions on resetting \
your password has been sent!"