Skip to content

Commit

Permalink
Update response meessages to front-end
Browse files Browse the repository at this point in the history
  • Loading branch information
Mhdez221993 committed Apr 4, 2022
1 parent 846127f commit 4f57306
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ $ rspec spec --format documentation
- [Testing a Rails API with RSpec](https://blog.devgenius.io/testing-a-rails-api-with-rspec-82dedc9f15df)
- [ruby on rails: heroku: Missing `secret_key_base` for 'production' environment](https://stackoverflow.com/questions/38167835/ruby-on-rails-heroku-missing-secret-key-base-for-production-environment)
- [Adding Swagger Docs to a Rails 6 API Using Rswag](https://medium.com/@clarkjohnson_85334/adding-swagger-docs-to-a-rails-6-api-using-rswag-a3e8bc577986)
- [Don't Forget to Expose Headers When Using Rack CORS](https://glaucocustodio.github.io/2016/01/20/dont-forget-to-expose-headers-when-using-rack-cors/)


## Authors
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/users/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ class Users::RegistrationsController < Devise::RegistrationsController
def respond_with(resource, _opts = {})
register_success && return if resource.persisted?

register_failed
register_failed(resource)
end

def register_success
render json: { message: 'Signed up sucessfully.' }
render json: { message: ['Sign Up Successfully!'], status: true }
end

def register_failed
render json: { message: 'Something went wrong.' }
def register_failed(resource)
render json: { message: resource.errors.full_messages, status: false }
end
end
16 changes: 13 additions & 3 deletions app/controllers/users/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ class Users::SessionsController < Devise::SessionsController
private

def respond_with(_resource, _opts = {})
render json: { message: 'You are logged in.' }, status: :ok
session_success && return if current_user

session_failed
end

def session_success
render json: { message: ['You are logged in.'], status: true }, status: :ok
end

def session_failed
render json: { message: ['Invalid Credentials'], status: false }
end

def respond_to_on_destroy
Expand All @@ -14,10 +24,10 @@ def respond_to_on_destroy
end

def log_out_success
render json: { message: 'You are logged out.' }, status: :ok
render json: { message: ['You are logged out.'] }, status: :ok
end

def log_out_failure
render json: { message: 'Hmm nothing happened.' }, status: :unauthorized
render json: { message: ['Hmm nothing happened.'] }, status: :unauthorized
end
end
2 changes: 1 addition & 1 deletion config/initializers/cors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

resource '*',
headers: :any,
methods: [:get, :post, :put, :patch, :delete, :options, :head]
methods: [:get, :post, :put, :patch, :delete, :options, :head], expose: ['Authorization']
end
end

0 comments on commit 4f57306

Please sign in to comment.