Skip to content

Commit

Permalink
response status at error resolve #1
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexei committed Mar 10, 2015
1 parent df227df commit 12702fc
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ def render_resource_or_errors(resource, options = {})
if resource.errors.empty?
render options.merge({ json: { resource: resource } })
else
render json: { errors: resource.errors }, status: :unprocessable_entity
render_errors resource.errors
end
end

def json_status(bool)
bool ? :ok : :error
bool ? :ok : :unprocessable_entity
end

def require_no_authentication
Expand All @@ -27,6 +27,11 @@ def require_no_authentication
def set_request_format!
request.format = :json
end

def render_errors(errors)
status = json_status !JwtAuthentication.status_error_in_response
render json: { errors: errors }, status: status
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class JwtAuthentication::ConfirmationsController < Devise::ConfirmationsControll
def create
self.resource = resource_class.send_confirmation_instructions(resource_params)
yield resource if block_given?
render json: { status: json_status(true) }
render nothing: true, status: json_status(true)
end

def show
Expand All @@ -13,7 +13,7 @@ def show
if resource.errors.empty?
render json: { auth_token: resource.jwt_token }
else
render json: { errors: resource.errors }
render_errors resource.errors
end
end
end
6 changes: 3 additions & 3 deletions app/controllers/jwt_authentication/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ def create
self.resource = resource_class.send_reset_password_instructions(resource_params)
yield resource if block_given?
if resource.errors.empty?
render json: { status: :ok }
render nothing: true, status: json_status(true)
else
render json: { errors: resource.errors }
render_errors resource.errors
end
end

Expand All @@ -19,7 +19,7 @@ def update
sign_in(resource_name, resource)
render json: { auth_token: resource.jwt_token }
else
render json: { errors: resource.errors }
render_errors resource.errors
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ def destroy
resource.destroy
Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
yield resource if block_given?
render json: { status: json_status(resource.destroyed?) }
render nothing: true, status: json_status(resource.destroyed?)
end
end
2 changes: 1 addition & 1 deletion app/controllers/jwt_authentication/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def destroy
Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
yield if block_given?
end
render json: { status: json_status(true) }
render nothing: true, status: json_status(true)
end

def destroy_all
Expand Down
3 changes: 3 additions & 0 deletions lib/generators/templates/jwt_authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@
# # acts_as_jwt_authenticatable key_fields: [:email, :id]
# config.key_fields = [:email]
#
# # Configure response http-status at fail. If true - status at error will be 422, if false - 200
# config.status_error_in_response = false
#

end
2 changes: 2 additions & 0 deletions lib/jwt_authentication/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Configuration
mattr_accessor :jwt_timeout
mattr_accessor :jwt_timeout_remember_me
mattr_accessor :key_fields
mattr_accessor :status_error_in_response

@@controller_adapters = ['rails', 'rails_api']
@@model_adapters = ['active_record', 'mongoid']
Expand All @@ -25,6 +26,7 @@ module Configuration
param_name: 'user_token',
sign_in: :devise}}
@@key_fields = [:email]
@@status_error_in_response = false

def configure
yield self if block_given?
Expand Down
2 changes: 1 addition & 1 deletion lib/jwt_authentication/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module JwtAuthentication
VERSION = '0.0.4'
VERSION = '0.0.5'
end

0 comments on commit 12702fc

Please sign in to comment.