Handle user password with turbo stream

This commit is contained in:
wandji20
2024-11-26 10:24:56 +01:00
committed by David Cook
parent e3431c7954
commit 3fd007fa3d
4 changed files with 14 additions and 21 deletions

View File

@@ -1,8 +1,6 @@
# frozen_string_literal: true
class UserPasswordsController < Spree::UserPasswordsController
include CablecarResponses
layout 'darkswarm'
def create
@@ -11,27 +9,20 @@ class UserPasswordsController < Spree::UserPasswordsController
self.resource = resource_class.send_reset_password_instructions(raw_params[resource_name])
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]
render :create
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]
render :create, status: :not_found
end
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 :create, status: :unprocessable_entity
end
def user_unconfirmed?

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

@@ -0,0 +1,2 @@
= turbo_stream.update 'forgot-feedback' do
= render partial: 'layouts/alert', locals: { type: @type, message: @message, tab: @tab, unconfirmed: @unconfirmed, email: params.dig(:spree_user, :email) }

View File

@@ -14,20 +14,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!"