Handle user session responses with turbo_stream

This commit is contained in:
wandji20
2024-10-13 00:48:40 +01:00
parent 3756e368c8
commit 996057b940
4 changed files with 28 additions and 18 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,15 @@ 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 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')
respond_to do |format|
format.html { head :unauthorized }
format.turbo_stream do
render :create, status: :unauthorized
end
end
end
end
@@ -60,11 +60,19 @@ 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)
local_values = { type: "alert", message:, unconfirmed: true,
tab: "login", email: params.dig(:spree_user, :email) }
respond_to do |format|
format.html { head :unprocessable_entity }
format.turbo_stream do
render turbo_stream: [
turbo_stream.update(
"login-feedback", partial: "layouts/alert", locals: local_values
)
], status: :unprocessable_entity
end
end
end
def ensure_valid_locale_persisted

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]

View File

@@ -0,0 +1,2 @@
= turbo_stream.update 'login-feedback' do
= render partial: 'layouts/alert', locals: { message: @message, type: 'alert' }

View File

@@ -14,8 +14,7 @@ RSpec.describe Spree::UserSessionsController, type: :controller do
context "when referer is not '/checkout'" 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).to have_http_status(:found)
expect(response.body).to match(root_path).and match("redirect")
end
end
@@ -26,7 +25,7 @@ RSpec.describe Spree::UserSessionsController, type: :controller 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).to have_http_status(:found)
expect(response.body).to match(checkout_path).and match("redirect")
end
end
@@ -36,7 +35,8 @@ RSpec.describe Spree::UserSessionsController, type: :controller 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.body).to include "Invalid email or password"