Skip to content

Commit

Permalink
Fix issue of early return on authentications API
Browse files Browse the repository at this point in the history
  • Loading branch information
aidewoode committed Jul 20, 2022
1 parent 6e1638d commit 8d7d880
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
6 changes: 3 additions & 3 deletions app/controllers/api/v1/authentications_controller.rb
Expand Up @@ -10,13 +10,13 @@ def create
session = UserSession.new(session_params.merge({remember_me: true}).to_h)

if params[:with_session]
head :unauthorized unless session.save
return head :unauthorized unless session.save
else
head :unauthorized unless session.valid?
return head :unauthorized unless session.valid?
end

@current_user = User.find_by(email: session_params[:email])
head :unauthorized unless @current_user.present?
return head :unauthorized unless @current_user.present?

@current_user.regenerate_api_token if @current_user.api_token.blank?
end
Expand Down
14 changes: 13 additions & 1 deletion app/controllers/application_controller.rb
Expand Up @@ -3,6 +3,8 @@
class ApplicationController < ActionController::Base
include SessionsHelper

helper_method :turbo_native?

before_action :find_current_user
before_action :require_login

Expand Down Expand Up @@ -54,7 +56,13 @@ def find_current_user
end

def require_login
redirect_to new_session_path unless logged_in?
return if logged_in?

if turbo_native?
head :unauthorized
else
redirect_to new_session_path
end
end

def require_admin
Expand All @@ -67,4 +75,8 @@ def logout_current_user

redirect_to new_session_path
end

def turbo_native?
request.user_agent.to_s.match?(/Turbo Native/)
end
end
4 changes: 0 additions & 4 deletions app/helpers/application_helper.rb
Expand Up @@ -82,8 +82,4 @@ def shelf_grid_tag(**options, &block)
def page_title_tag(title)
content_for :title, title
end

def turbo_native?
request.user_agent.to_s.match?(/Turbo Native/)
end
end
4 changes: 2 additions & 2 deletions test/controllers/api/v1/authentications_controller_test.rb
Expand Up @@ -41,7 +41,7 @@ class Api::V1::AuthenticationsControllerTest < ActionDispatch::IntegrationTest
test "should not create authentication with wrong credential" do
post api_v1_authentication_url, as: :json, params: {
user_session: {
email: @user.email,
email: "fake@email.com",
password: "fake"
}
}
Expand All @@ -55,7 +55,7 @@ class Api::V1::AuthenticationsControllerTest < ActionDispatch::IntegrationTest
post api_v1_authentication_url, as: :json, params: {
with_session: true,
user_session: {
email: @user.email,
email: "fake@email.com",
password: "fake"
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/controllers/application_controller_test.rb
Expand Up @@ -32,4 +32,12 @@ class ApplicationControllerTest < ActionDispatch::IntegrationTest
get "/dummy_index"
assert_response :success
end

test "should get unauthorized when did not logged in on turbo native agent" do
get "/dummy_index", headers: {"User-Agent" => "Turbo Native iOS"}
assert_response :unauthorized

get "/dummy_index", headers: {"User-Agent" => "Turbo Native Android"}
assert_response :unauthorized
end
end

0 comments on commit 8d7d880

Please sign in to comment.