Disable guest checkout for registered users

This commit is contained in:
Matt-Yorkley
2018-02-07 21:00:02 +00:00
committed by Maikel Linke
parent 4551fa60c5
commit 598677be3f
9 changed files with 110 additions and 44 deletions

View File

@@ -1,6 +1,13 @@
Darkswarm.controller "LoginCtrl", ($scope, $timeout, $location, $http, $window, AuthenticationService, Redirections, Loading) ->
$scope.path = "/login"
$scope.modalMessage = null
$scope.$watch (->
AuthenticationService.modalMessage
), (newValue) ->
$scope.errors = newValue
$scope.submit = ->
Loading.message = t 'logging_in'
$http.post("/user/spree_user/sign_in", {spree_user: $scope.spree_user}).success (data)->

View File

@@ -1,4 +1,4 @@
Darkswarm.controller "CheckoutCtrl", ($scope, localStorageService, Checkout, CurrentUser, CurrentHub) ->
Darkswarm.controller "CheckoutCtrl", ($scope, localStorageService, Checkout, CurrentUser, CurrentHub, AuthenticationService, SpreeUser, $http) ->
$scope.Checkout = Checkout
$scope.submitted = false
@@ -21,7 +21,26 @@ Darkswarm.controller "CheckoutCtrl", ($scope, localStorageService, Checkout, Cur
$scope.purchase = (event, form) ->
event.preventDefault()
$scope.submitted = true
if CurrentUser.id
$scope.validateForm(form)
else
$scope.confirmGuest(true)
$scope.validateForm = (form) ->
if form.$valid
$scope.Checkout.purchase()
else
$scope.$broadcast 'purchaseFormInvalid', form
$scope.confirmGuest = ->
$http.post("/user/registered_email", {email: $scope.order.email}).success (data)->
if data.registered == true
$scope.promptLogin()
else
$scope.validateForm() if $scope.submitted
$scope.promptLogin = ->
SpreeUser.spree_user.email = $scope.order.email
AuthenticationService.pushMessage t('devise.failure.already_registered')
AuthenticationService.open '/login'

View File

@@ -1,8 +1,15 @@
Darkswarm.controller "DetailsCtrl", ($scope, $timeout) ->
Darkswarm.controller "DetailsCtrl", ($scope, $timeout, $http, CurrentUser, AuthenticationService, SpreeUser) ->
angular.extend(this, new FieldsetMixin($scope))
$scope.name = "details"
$scope.nextPanel = "billing"
$scope.login_or_next = (event) ->
event.preventDefault()
unless CurrentUser.id
$scope.confirmGuest()
$scope.next()
$scope.summary = ->
[$scope.fullName(),
$scope.order.email,

View File

@@ -2,6 +2,7 @@ Darkswarm.factory "AuthenticationService", (Navigation, $modal, $location, Redir
new class AuthenticationService
selectedPath: "/login"
modalMessage: null
constructor: ->
if $location.path() in ["/login", "/signup", "/forgot"] || location.pathname is '/register/auth'
@@ -32,6 +33,8 @@ Darkswarm.factory "AuthenticationService", (Navigation, $modal, $location, Redir
'registration_authentication.html'
else
'authentication.html'
pushMessage: (message) ->
@modalMessage = String(message)
select: (path)=>
@selectedPath = path

View File

@@ -15,4 +15,10 @@ Spree::UsersController.class_eval do
@orders = @orders.where('distributor_id != ?', Spree::Config.accounts_distributor_id)
end
# Endpoint for queries to check if a user is already registered
def registered_email
user = Spree.user_class.find_by_email params[:email]
render json: { registered: user.present? }
end
end

View File

@@ -28,5 +28,5 @@
.row
.small-12.columns.text-right
%button.primary{"ng-disabled" => "details.$invalid", "ng-click" => "next($event)"}
%button.primary{"ng-disabled" => "details.$invalid", "ng-click" => "login_or_next($event)"}
= t :next

View File

@@ -117,6 +117,7 @@ en:
Invalid email or password.
Were you a guest last time? Perhaps you need to create an account or reset your password.
unconfirmed: "You have to confirm your account before continuing."
already_registered: "This email address is already registered. Please log in to continue, or go back and use another email address."
enterprise_mailer:
confirmation_instructions:
subject: "Please confirm the email address for %{enterprise}"

View File

@@ -19,6 +19,7 @@ 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"
# Redirects to global website
get "/connect", to: redirect("https://openfoodnetwork.org/#{ENV['DEFAULT_COUNTRY_CODE'].andand.downcase}/connect/")

View File

@@ -7,54 +7,76 @@ feature "As a consumer I want to check out my cart", js: true do
include CheckoutWorkflow
include UIComponentHelper
let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) }
let(:supplier) { create(:supplier_enterprise) }
let!(:order_cycle) { create(:simple_order_cycle, distributors: [distributor], coordinator: create(:distributor_enterprise), variants: [product.variants.first]) }
let(:product) { create(:simple_product, supplier: supplier) }
let(:order) { create(:order, order_cycle: order_cycle, distributor: distributor) }
let(:address) { create(:address, firstname: "Foo", lastname: "Bar") }
let(:user) { create(:user, bill_address: address, ship_address: address) }
after { Warden.test_reset! }
describe "using the checkout" do
let(:distributor) { create(:distributor_enterprise, with_payment_and_shipping: true) }
let(:supplier) { create(:supplier_enterprise) }
let!(:order_cycle) { create(:simple_order_cycle, distributors: [distributor], coordinator: create(:distributor_enterprise), variants: [product.variants.first]) }
let(:product) { create(:simple_product, supplier: supplier) }
let(:order) { create(:order, order_cycle: order_cycle, distributor: distributor) }
let(:address) { create(:address, firstname: "Foo", lastname: "Bar") }
let(:user) { create(:user, bill_address: address, ship_address: address) }
before do
set_order order
add_product_to_cart order, product
end
after { Warden.test_reset! }
it "does not not render the login form when logged in" do
quick_login_as user
visit checkout_path
within "section[role='main']" do
page.should_not have_content "Login"
page.should have_checkout_details
before do
set_order order
add_product_to_cart order, product
end
end
it "renders the login buttons when logged out" do
visit checkout_path
within "section[role='main']" do
page.should have_content "Login"
click_button "Login"
it "does not not render the login form when logged in" do
quick_login_as user
visit checkout_path
within "section[role='main']" do
page.should_not have_content "Login"
page.should have_checkout_details
end
end
page.should have_login_modal
end
it "populates user details once logged in" do
visit checkout_path
within("section[role='main']") { click_button "Login" }
page.should have_login_modal
fill_in "Email", with: user.email
fill_in "Password", with: user.password
within(".login-modal") { click_button 'Login' }
toggle_details
it "renders the login buttons when logged out" do
visit checkout_path
within "section[role='main']" do
page.should have_content "Login"
click_button "Login"
end
page.should have_login_modal
end
page.should have_field 'First Name', with: 'Foo'
page.should have_field 'Last Name', with: 'Bar'
end
it "populates user details once logged in" do
visit checkout_path
within("section[role='main']") { click_button "Login" }
page.should have_login_modal
fill_in "Email", with: user.email
fill_in "Password", with: user.password
within(".login-modal") { click_button 'Login' }
toggle_details
it "allows user to checkout as guest" do
visit checkout_path
checkout_as_guest
page.should have_checkout_details
page.should have_field 'First Name', with: 'Foo'
page.should have_field 'Last Name', with: 'Bar'
end
context "using the guest checkout" do
it "allows user to checkout as guest" do
visit checkout_path
checkout_as_guest
page.should have_checkout_details
end
it "asks the user to log in if they are using a registered email" do
visit checkout_path
checkout_as_guest
fill_in 'First Name', with: 'Not'
fill_in 'Last Name', with: 'Guest'
fill_in 'Email', with: user.email
fill_in 'Phone', with: '098712736'
within '#details' do
click_button 'Next'
end
expect(page).to have_selector 'div.login-modal', visible: true
expect(page).to have_content I18n.t('devise.failure.already_registered')
end
end
end
end