Skip to content

Commit

Permalink
fix auth endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
mattlindsey committed Dec 14, 2020
1 parent 1e9e8aa commit f2f9267
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
24 changes: 20 additions & 4 deletions app/controllers/registrations_controller.rb
Expand Up @@ -2,14 +2,30 @@

class RegistrationsController < Devise::RegistrationsController
respond_to :json
protect_from_forgery with: :null_session

def validation_error(resource)
render json: {
errors: [
{
status: '400',
title: 'Bad Request',
detail: resource.errors,
code: '100'
}
]
}, status: :bad_request
end

def create
build_resource(sign_up_params)
if resource.save
sign_in(resource)
head :created
resource.save
sign_in(resource)

if resource.errors.empty?
render json: resource
else
render_resource(resource)
validation_error(resource)
end
end
end
3 changes: 2 additions & 1 deletion app/controllers/sessions_controller.rb
Expand Up @@ -2,7 +2,8 @@

class SessionsController < Devise::SessionsController
respond_to :json

protect_from_forgery with: :null_session

private

def respond_with(resource, _opts={})
Expand Down
14 changes: 7 additions & 7 deletions spec/requests/registrations_spec.rb
Expand Up @@ -55,12 +55,12 @@
end
end

#describe 'error' do
# it 'responds with error with invalid params' do
# post endpoint, params: invalid_params
# json_response = response.parsed_body
# expect(json_response['errors'][0]['detail'].keys).to match_array(%w[email password password_confirmation])
# end
#end
describe 'error' do
it 'responds to error with message' do
post endpoint, params: invalid_params
json_response = response.parsed_body
expect(json_response['errors'][0]['title']).to eq('Bad Request')
end
end
end
end

0 comments on commit f2f9267

Please sign in to comment.