From 67e72f4504d04cb4a8894dcb4162f96c1e338014 Mon Sep 17 00:00:00 2001 From: Will Marshall Date: Wed, 21 May 2014 16:39:59 +1000 Subject: [PATCH] Setting up after_login redirects --- .../authentication/login_controller.js.coffee | 7 ++++-- .../services/authentication_service.js.coffee | 2 +- .../darkswarm/services/redirections.js.coffee | 3 +++ .../admin/overview_controller_decorator.rb | 3 +-- app/helpers/shared_helper.rb | 5 +++++ app/views/json/_injection.html.haml | 2 ++ app/views/layouts/darkswarm.html.haml | 8 +++---- app/views/shared/_current_hub.haml | 2 -- app/views/shared/_current_user.haml | 2 -- spec/features/consumer/authentication_spec.rb | 22 ++++++++++++++++++- 10 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 app/assets/javascripts/darkswarm/services/redirections.js.coffee create mode 100644 app/views/json/_injection.html.haml delete mode 100644 app/views/shared/_current_hub.haml delete mode 100644 app/views/shared/_current_user.haml 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 c2211bc033..da625e3d34 100644 --- a/app/assets/javascripts/darkswarm/controllers/authentication/login_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/authentication/login_controller.js.coffee @@ -1,8 +1,11 @@ -Darkswarm.controller "LoginCtrl", ($scope, $http, $location, AuthenticationService) -> +Darkswarm.controller "LoginCtrl", ($scope, $http, AuthenticationService, Redirections) -> $scope.path = "/login" $scope.submit = -> $http.post("/user/spree_user/sign_in", {spree_user: $scope.spree_user}).success (data)-> - location.href = location.origin + location.pathname # Strips out hash fragments + if Redirections.after_login + location.href = location.origin + Redirections.after_login + else + location.href = location.origin + location.pathname # Strips out hash fragments .error (data) -> $scope.errors = data.message diff --git a/app/assets/javascripts/darkswarm/services/authentication_service.js.coffee b/app/assets/javascripts/darkswarm/services/authentication_service.js.coffee index eada8bc44b..820d5bde9c 100644 --- a/app/assets/javascripts/darkswarm/services/authentication_service.js.coffee +++ b/app/assets/javascripts/darkswarm/services/authentication_service.js.coffee @@ -1,4 +1,4 @@ -Darkswarm.factory "AuthenticationService", (Navigation, $modal, $location)-> +Darkswarm.factory "AuthenticationService", (Navigation, $modal, $location, Redirections)-> new class AuthenticationService selectedPath: "/login" diff --git a/app/assets/javascripts/darkswarm/services/redirections.js.coffee b/app/assets/javascripts/darkswarm/services/redirections.js.coffee new file mode 100644 index 0000000000..a479964e9e --- /dev/null +++ b/app/assets/javascripts/darkswarm/services/redirections.js.coffee @@ -0,0 +1,3 @@ +Darkswarm.factory "Redirections", ($location)-> + new class Redirections + after_login: $location.search().after_login diff --git a/app/controllers/spree/admin/overview_controller_decorator.rb b/app/controllers/spree/admin/overview_controller_decorator.rb index 5c96901c1d..a6142d26c0 100644 --- a/app/controllers/spree/admin/overview_controller_decorator.rb +++ b/app/controllers/spree/admin/overview_controller_decorator.rb @@ -13,8 +13,7 @@ Spree::Admin::OverviewController.class_eval do redirect_to '/unauthorized' else store_location - url = respond_to?(:spree_login_path) ? spree_login_path : root_path - redirect_to url + redirect_to root_path(anchor: "login?after_login=#{spree.admin_path}") end end end diff --git a/app/helpers/shared_helper.rb b/app/helpers/shared_helper.rb index be1e0ba0f4..828277cb57 100644 --- a/app/helpers/shared_helper.rb +++ b/app/helpers/shared_helper.rb @@ -1,4 +1,9 @@ module SharedHelper + + def inject_json(name, partial) + render "json/injection", name: name, partial: partial + end + def distributor_link_class(distributor) cart = current_order(true) @active_distributors ||= Enterprise.distributors_with_active_order_cycles diff --git a/app/views/json/_injection.html.haml b/app/views/json/_injection.html.haml new file mode 100644 index 0000000000..95b921406b --- /dev/null +++ b/app/views/json/_injection.html.haml @@ -0,0 +1,2 @@ +:javascript + angular.module('Darkswarm').value("#{name.to_s}", #{render "json/#{partial.to_s}"}) diff --git a/app/views/layouts/darkswarm.html.haml b/app/views/layouts/darkswarm.html.haml index 5c2dcaee66..edeffafeed 100644 --- a/app/views/layouts/darkswarm.html.haml +++ b/app/views/layouts/darkswarm.html.haml @@ -15,16 +15,16 @@ = csrf_meta_tags %body.off-canvas{"ng-app" => "Darkswarm"} + = inject_json "currentHub", "current_hub" + = inject_json "user", "current_user" + .off-canvas-wrap{offcanvas: true} .inner-wrap - = render partial: "shared/current_hub" - = render partial: "shared/current_user" = render partial: "shared/menu/menu" + = display_flash_messages %ofn-flash - -#= render "shared/sidebar" - %section{ role: "main" } = yield diff --git a/app/views/shared/_current_hub.haml b/app/views/shared/_current_hub.haml deleted file mode 100644 index 5a0ff7c564..0000000000 --- a/app/views/shared/_current_hub.haml +++ /dev/null @@ -1,2 +0,0 @@ -:javascript - angular.module('Darkswarm').value('currentHub', #{render "json/current_hub"}) diff --git a/app/views/shared/_current_user.haml b/app/views/shared/_current_user.haml deleted file mode 100644 index 9745a71313..0000000000 --- a/app/views/shared/_current_user.haml +++ /dev/null @@ -1,2 +0,0 @@ -:javascript - angular.module('Darkswarm').value('user', #{render "json/current_user"}) diff --git a/spec/features/consumer/authentication_spec.rb b/spec/features/consumer/authentication_spec.rb index 77b60290d1..76e4a20877 100644 --- a/spec/features/consumer/authentication_spec.rb +++ b/spec/features/consumer/authentication_spec.rb @@ -5,7 +5,27 @@ feature "Authentication", js: true do describe "login" do let(:user) { create(:user, password: "password", password_confirmation: "password") } - describe "newskool" do + describe "With redirects" do + scenario "logging in with a redirect set" do + visit groups_path(anchor: "login?after_login=#{producers_path}") + fill_in "Email", with: user.email + fill_in "Password", with: user.password + click_login_button + page.should have_content "Select a producer from the list below" + current_path.should == producers_path + end + + scenario "logging into admin redirects home, then back to admin" do + visit spree.admin_path + fill_in "Email", with: user.email + fill_in "Password", with: user.password + click_login_button + page.should have_content "Dashboard" + current_path.should == spree.admin_path + end + end + + describe "Loggin in from the home page" do before do visit root_path end