diff --git a/app/controllers/spree/user_sessions_controller.rb b/app/controllers/spree/user_sessions_controller.rb index c22c6162fd..c3880fc28b 100644 --- a/app/controllers/spree/user_sessions_controller.rb +++ b/app/controllers/spree/user_sessions_controller.rb @@ -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 diff --git a/app/controllers/spree/users_controller.rb b/app/controllers/spree/users_controller.rb index 6d22dc86e3..583fb13087 100644 --- a/app/controllers/spree/users_controller.rb +++ b/app/controllers/spree/users_controller.rb @@ -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 diff --git a/app/controllers/user_confirmations_controller.rb b/app/controllers/user_confirmations_controller.rb index d8b6b221fe..ee65e9c6a9 100644 --- a/app/controllers/user_confirmations_controller.rb +++ b/app/controllers/user_confirmations_controller.rb @@ -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 } diff --git a/app/controllers/user_passwords_controller.rb b/app/controllers/user_passwords_controller.rb index b0ad3970d1..72f947fec9 100644 --- a/app/controllers/user_passwords_controller.rb +++ b/app/controllers/user_passwords_controller.rb @@ -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? diff --git a/app/views/layouts/_alert.html.haml b/app/views/layouts/_alert.html.haml index 36db8b7e31..c77aeb80f1 100644 --- a/app/views/layouts/_alert.html.haml +++ b/app/views/layouts/_alert.html.haml @@ -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') diff --git a/app/views/layouts/_forgot_tab.html.haml b/app/views/layouts/_forgot_tab.html.haml index 47ad3c03d2..c1b8e36e88 100644 --- a/app/views/layouts/_forgot_tab.html.haml +++ b/app/views/layouts/_forgot_tab.html.haml @@ -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 } diff --git a/app/views/layouts/_login_tab.html.haml b/app/views/layouts/_login_tab.html.haml index a80a39acce..a1a21f2647 100644 --- a/app/views/layouts/_login_tab.html.haml +++ b/app/views/layouts/_login_tab.html.haml @@ -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) diff --git a/app/views/layouts/_signup_tab.html.haml b/app/views/layouts/_signup_tab.html.haml index f25bf2024d..929e56c58f 100644 --- a/app/views/layouts/_signup_tab.html.haml +++ b/app/views/layouts/_signup_tab.html.haml @@ -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 diff --git a/app/webpacker/controllers/login_modal_controller.js b/app/webpacker/controllers/login_modal_controller.js index 92c3f6516d..8ef8f049f0 100644 --- a/app/webpacker/controllers/login_modal_controller.js +++ b/app/webpacker/controllers/login_modal_controller.js @@ -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 = "/"; } diff --git a/config/routes.rb b/config/routes.rb index abc399163b..555718df81 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/controllers/spree/user_sessions_controller_spec.rb b/spec/controllers/spree/user_sessions_controller_spec.rb index 976b0d5c51..fc30f98314 100644 --- a/spec/controllers/spree/user_sessions_controller_spec.rb +++ b/spec/controllers/spree/user_sessions_controller_spec.rb @@ -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 diff --git a/spec/controllers/spree/users_controller_spec.rb b/spec/controllers/spree/users_controller_spec.rb index 3e878f6778..afd39a0a7b 100644 --- a/spec/controllers/spree/users_controller_spec.rb +++ b/spec/controllers/spree/users_controller_spec.rb @@ -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) diff --git a/spec/controllers/user_passwords_controller_spec.rb b/spec/controllers/user_passwords_controller_spec.rb index 9c9da7349e..eb1ce6e8af 100644 --- a/spec/controllers/user_passwords_controller_spec.rb +++ b/spec/controllers/user_passwords_controller_spec.rb @@ -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!"