Handle user sessions with turbo stream

This commit is contained in:
wandji20
2024-11-26 10:24:20 +01:00
committed by David Cook
parent 9fa715c709
commit e3431c7954
6 changed files with 21 additions and 20 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,10 @@ 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')
render :create, status: :unauthorized
end
end
@@ -60,11 +55,11 @@ 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) }
render :unconfirmed_user, status: :unprocessable_entity
end
def ensure_valid_locale_persisted

View File

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

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)
@@ -21,4 +21,4 @@
= form.label :remember_me, t(:remember_me)
.row
.large-12.columns
= form.submit t(:label_login), { class: "button primary", tabindex: 4 }
= form.submit t(:label_login), { class: "button primary", tabindex: 4 }

View File

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

View File

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

View File

@@ -15,7 +15,7 @@ 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).to have_http_status(:found)
expect(response.body).to match(root_path).and match("redirect")
end
end
@@ -26,7 +26,7 @@ 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).to have_http_status(:found)
expect(response.body).to match(checkout_path).and match("redirect")
end
end
@@ -36,7 +36,8 @@ 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.body).to include "Invalid email or password"