Skip to content

Commit 6207b0a

Browse files
committed
Adding cors and adjust response auth
1 parent 354cfaa commit 6207b0a

6 files changed

Lines changed: 15 additions & 4 deletions

File tree

pdv/Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ gem 'bcrypt'
2525
gem 'jwt'
2626
# Use simple command for create services
2727
gem 'simple_command'
28+
# Use to active cors
29+
gem 'rack-cors', require: 'rack/cors'
2830
# Use ActiveStorage variant
2931
# gem 'mini_magick', '~> 4.8'
3032

pdv/Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ GEM
8282
pg (0.18.4)
8383
puma (3.12.0)
8484
rack (2.0.6)
85+
rack-cors (1.0.2)
8586
rack-test (1.1.0)
8687
rack (>= 1.0, < 3)
8788
rails (5.2.2)
@@ -147,6 +148,7 @@ DEPENDENCIES
147148
listen (>= 3.0.5, < 3.2)
148149
pg (~> 0.18.4)
149150
puma (~> 3.11)
151+
rack-cors
150152
rails (~> 5.2.2)
151153
simple_command
152154
spring

pdv/app/commands/authenticate_user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def user
1818
user = User.find_by_username(username)
1919
return user if user && user.authenticate(password)
2020

21-
errors.add :user_authentication, 'invalid credentials'
21+
errors.add :message, 'Invalid credentials'
2222
nil
2323
end
2424
end

pdv/app/controllers/auth_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ def authenticate
1111
access: user.access,
1212
auth_token: command.result
1313
}
14-
render json: response, status: 200
14+
render json: { data: response } , status: 200
1515
else
16-
render json: { error: command.errors }, status: :unauthorized
16+
render json: { errors: command.errors }, status: :unauthorized
1717
end
1818
end
1919

pdv/config/application.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,12 @@ class Application < Rails::Application
3131
# Middleware like session, flash, cookies can be added back manually.
3232
# Skip views, helpers and assets when generating a new resource.
3333
config.api_only = true
34+
35+
config.middleware.insert_before 0, Rack::Cors do
36+
allow do
37+
origins '*'
38+
resource '*', headers: :any, methods: [:get, :post, :options]
39+
end
40+
end
3441
end
3542
end

pdv/config/puma.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
1111
#
12-
port ENV.fetch("PORT") { 3000 }
12+
port ENV.fetch("PORT") { 3004 }
1313

1414
# Specifies the `environment` that Puma will run in.
1515
#

0 commit comments

Comments
 (0)