diff --git a/app/assets/javascripts/darkswarm/controllers/authentication/login_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/authentication/login_controller.js.coffee index f1bb13bcca..c6c54e38d1 100644 --- a/app/assets/javascripts/darkswarm/controllers/authentication/login_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/authentication/login_controller.js.coffee @@ -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)-> diff --git a/app/assets/javascripts/darkswarm/controllers/checkout/checkout_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/checkout/checkout_controller.js.coffee index 3397077a00..4f18572436 100644 --- a/app/assets/javascripts/darkswarm/controllers/checkout/checkout_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/checkout/checkout_controller.js.coffee @@ -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' diff --git a/app/assets/javascripts/darkswarm/controllers/checkout/details_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/checkout/details_controller.js.coffee index e0fef8e343..dcc6b44c63 100644 --- a/app/assets/javascripts/darkswarm/controllers/checkout/details_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/checkout/details_controller.js.coffee @@ -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, diff --git a/app/assets/javascripts/darkswarm/services/authentication_service.js.coffee b/app/assets/javascripts/darkswarm/services/authentication_service.js.coffee index b504d92c76..a51ccdedee 100644 --- a/app/assets/javascripts/darkswarm/services/authentication_service.js.coffee +++ b/app/assets/javascripts/darkswarm/services/authentication_service.js.coffee @@ -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 diff --git a/app/controllers/spree/users_controller_decorator.rb b/app/controllers/spree/users_controller_decorator.rb index aca371c75b..53cfd09324 100644 --- a/app/controllers/spree/users_controller_decorator.rb +++ b/app/controllers/spree/users_controller_decorator.rb @@ -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 diff --git a/app/views/checkout/_details.html.haml b/app/views/checkout/_details.html.haml index 788996be37..775c00c670 100644 --- a/app/views/checkout/_details.html.haml +++ b/app/views/checkout/_details.html.haml @@ -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 diff --git a/config/locales/en.yml b/config/locales/en.yml index 4a2ee53afa..bf51c438ef 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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}" diff --git a/config/routes.rb b/config/routes.rb index 2062a1d60f..fb4b652bac 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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/") diff --git a/spec/features/consumer/shopping/checkout_auth_spec.rb b/spec/features/consumer/shopping/checkout_auth_spec.rb index e23e4c15a8..517144ed81 100644 --- a/spec/features/consumer/shopping/checkout_auth_spec.rb +++ b/spec/features/consumer/shopping/checkout_auth_spec.rb @@ -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