diff --git a/app/assets/javascripts/darkswarm/controllers/forgot_sidebar_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/forgot_sidebar_controller.js.coffee index 075b4f0912..f740e5aaf8 100644 --- a/app/assets/javascripts/darkswarm/controllers/forgot_sidebar_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/forgot_sidebar_controller.js.coffee @@ -1,6 +1,21 @@ window.ForgotSidebarCtrl = Darkswarm.controller "ForgotSidebarCtrl", ($scope, $http, $location) -> + $scope.spree_user = { + email: null + } + $scope.active = -> $location.path() == '/forgot' $scope.select = -> $location.path("/forgot") + + $scope.submit = -> + if $scope.spree_user.email != null + $http.post("/user/spree_user/password", {spree_user: $scope.spree_user}).success (data)-> + + $location.path("/reset") + + .error (data) -> + $scope.errors = "Email address not found" + else + $scope.errors = "You must provide an email address" diff --git a/app/controllers/user_passwords_controller.rb b/app/controllers/user_passwords_controller.rb index 1a357a6c37..8e6f599523 100644 --- a/app/controllers/user_passwords_controller.rb +++ b/app/controllers/user_passwords_controller.rb @@ -1,3 +1,20 @@ class UserPasswordsController < Spree::UserPasswordsController + def create + self.resource = resource_class.send_reset_password_instructions(params[resource_name]) + + if resource.errors.empty? + set_flash_message(:notice, :send_instructions) if is_navigational_format? + respond_with resource, :location => spree.login_path + else + respond_to do |format| + format.html do + respond_with_navigational(resource) { render :new } + end + format.js do + render json: resource.errors, status: :unauthorized + end + end + end + end end diff --git a/app/views/shared/_forgot_sidebar.html.haml b/app/views/shared/_forgot_sidebar.html.haml index 8f1f336bbd..84b0540f1a 100644 --- a/app/views/shared/_forgot_sidebar.html.haml +++ b/app/views/shared/_forgot_sidebar.html.haml @@ -2,4 +2,21 @@ heading: "Forgot Password?", active: "active()", select: "select()"} - Well you're a bit stupid then + + {{ spree_user.email }} + %form{"ng-submit" => "submit()"} + .alert-box.alert{"ng-show" => "errors != null"} + {{ errors }} + .row + .large-12.columns + %label{for: "email"} Email + %input.title.input-text{name: "email", + type: "email", + tabindex: 1, + "ng-model" => "spree_user.email"} + .row + .large-12.columns + %input.button.primary{name: "commit", + tabindex: "3", + type: "submit", + value: "Reset password"} diff --git a/config/environments/development.rb b/config/environments/development.rb index c90b82ca82..62642f7809 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -30,6 +30,7 @@ Openfoodnetwork::Application.configure do # Show emails using Letter Opener config.action_mailer.delivery_method = :letter_opener + config.action_mailer.default_url_options = { host: "test.com" } end diff --git a/config/routes.rb b/config/routes.rb index 76981588f8..0e3db0bd35 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -75,7 +75,7 @@ Spree::Core::Engine.routes.draw do :class_name => 'Spree::User', :controllers => { :sessions => 'spree/user_sessions', :registrations => 'user_registrations', - :passwords => 'spree/user_passwords' }, + :passwords => 'user_passwords' }, :skip => [:unlocks, :omniauth_callbacks], :path_names => { :sign_out => 'logout' }, :path_prefix => :user diff --git a/spec/controllers/user_passwords_controller_spec.rb b/spec/controllers/user_passwords_controller_spec.rb index 1417fca80e..e79a991b39 100644 --- a/spec/controllers/user_passwords_controller_spec.rb +++ b/spec/controllers/user_passwords_controller_spec.rb @@ -8,7 +8,7 @@ describe UserPasswordsController do ActionMailer::Base.default_url_options[:host] = "test.host" end - it "returns errors when no data received" do + it "returns errors" do spree_post :create, spree_user: {} response.should be_success response.should render_template "spree/user_passwords/new" @@ -20,5 +20,12 @@ describe UserPasswordsController do response.should be_redirect end + describe "via ajax" do + it "returns errors" do + xhr :post, :create, spree_user: {}, use_route: :spree + json = JSON.parse(response.body) + response.status.should == 401 + json.should == {"email"=>["can't be blank"]} + end + end end -