Skip to content

Commit 683db03

Browse files
committed
do not redirect if format is :js
1 parent 9bdef95 commit 683db03

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

lib/kracken/controllers/authenticatable.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ def authenticate_user
3838
def authenticate_user!
3939
check_token_expiry!
4040
unless user_signed_in?
41-
if request.format == :json
41+
case request.format
42+
when :json
4243
render json: {error: '401 Unauthorized'}, status: :unauthorized
44+
when :js
45+
head :unauthorized
4346
else
4447
redirect_to_sign_in
4548
end

spec/kracken/controllers/authenticatable_spec.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class ControllerDouble < BaseControllerDouble
4343
end
4444

4545
context "when no users are logged in" do
46-
it "#authenticate! redirects to root_url" do
47-
allow(controller).to receive(:request).and_return(double(format: nil, fullpath: nil))
46+
it "#authenticate! redirects to root_url for format html" do
47+
allow(controller).to receive(:request).and_return(double(format: :html, fullpath: nil))
4848
allow(controller).to receive(:redirect_to)
4949

5050
controller.authenticate_user!
@@ -56,6 +56,28 @@ class ControllerDouble < BaseControllerDouble
5656
expect(controller.user_signed_in?).to be_falsey
5757
end
5858

59+
it "#authenticate! doesn't redirect for format json" do
60+
allow(controller).to receive(:request).and_return(double(format: :json, fullpath: nil))
61+
allow(controller).to receive(:redirect_to)
62+
allow(controller).to receive(:render)
63+
64+
controller.authenticate_user!
65+
66+
expect(controller).not_to have_received(:redirect_to)
67+
expect(controller).to have_received(:render)
68+
end
69+
70+
it "#authenticate! doesn't redirect for format js" do
71+
allow(controller).to receive(:request).and_return(double(format: :js, fullpath: nil))
72+
allow(controller).to receive(:redirect_to)
73+
allow(controller).to receive(:head)
74+
75+
controller.authenticate_user!
76+
77+
expect(controller).not_to have_received(:redirect_to)
78+
expect(controller).to have_received(:head).with(:unauthorized)
79+
end
80+
5981
end
6082

6183
context "when a user is logged in" do

0 commit comments

Comments
 (0)