rails_jwt_auth 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 590d1c824e7a0171f9e8c084046dc0351181487d
4
- data.tar.gz: ef843896d84008e223cefa7fa805ef646bdd4926
3
+ metadata.gz: 6b162526193bd334a5f559b06df71fa65af2596e
4
+ data.tar.gz: 39edbb7292698a62f424a94807295d0cb3941dc8
5
5
  SHA512:
6
- metadata.gz: 80f81e20135779d4d172fce4d06d0f92cb21aee8a5b45a81a93e1d6d2fdba20dd6c37de4747315d8df5b6b13e08b44b78d7bc6d2f75c869af6708e0186d9da79
7
- data.tar.gz: 69a6d187020c8acb0984adbedbaed96aa45d622bea1ad63d9b657cd04dc769c6a8770fd2b0eb0b7509331e536cda01de58fa1072a05e270d49c2f1cf963e1968
6
+ metadata.gz: 56056babd43f0f71ef9b33990b4ba847986ae5d81dc49222f60f615709e6e64ebb4b2ff34551ae636008fe49171812e1d52bcb1bb84c60cb9a80495855886a1c
7
+ data.tar.gz: 2c715028b96c35fc280030471a61ea13e646c9bf3b2a87476ad0f57926e19ec46cd34c3fa345c7838436d7e952c086243c3e742e038c984d7b733240e8b4ddd3
data/README.md CHANGED
@@ -324,7 +324,7 @@ Password api is defined by RailsJwtAuth::PasswordsController.
324
324
 
325
325
  ## Custom controllers
326
326
 
327
- You can overwrite RailsJwtAuth controller to edit actions, responses,
327
+ You can overwrite RailsJwtAuth controllers to edit actions, responses,
328
328
  permitted parameters...
329
329
 
330
330
  For example, if we want to change registration strong parameters we
@@ -348,6 +348,10 @@ And edit route resource to use it:
348
348
  resource :registration, controller: 'registrations', only: [:create, :update, :destroy]
349
349
  ```
350
350
 
351
+ ## Custom responses
352
+
353
+ You can overwrite `RailsJwtAuth::RenderHelper` to customize controllers responses.
354
+
351
355
  ## Testing (rspec)
352
356
 
353
357
  Require the RailsJwtAuth::Spec::Helpers helper module in `rails_helper.rb`.
@@ -1,38 +1,25 @@
1
- class RailsJwtAuth::ConfirmationsController < ApplicationController
2
- def create
3
- user = RailsJwtAuth.model.where(email: confirmation_params[:email]).first
4
- return render json: create_error_response, status: 422 unless user
1
+ module RailsJwtAuth
2
+ class ConfirmationsController < ApplicationController
3
+ include RenderHelper
5
4
 
6
- user.send_confirmation_instructions
7
- render json: {}, status: 204
8
- end
9
-
10
- def update
11
- user = RailsJwtAuth.model.where(confirmation_token: params[:confirmation_token]).first
12
- return render json: update_error_response(nil), status: 422 unless user
5
+ def create
6
+ user = RailsJwtAuth.model.where(email: confirmation_params[:email]).first
7
+ return render_422(email: [I18n.t('rails_jwt_auth.errors.not_found')]) unless user
13
8
 
14
- if user.confirm!
15
- render json: {}, status: 204
16
- else
17
- render json: update_error_response(user), status: 422
9
+ user.send_confirmation_instructions ? render_204 : render_422(user.errors)
18
10
  end
19
- end
20
11
 
21
- private
12
+ def update
13
+ user = RailsJwtAuth.model.where(confirmation_token: params[:confirmation_token]).first
14
+ return render_422(confirmation_token: [I18n.t('rails_jwt_auth.errors.not_found')]) unless user
22
15
 
23
- def confirmation_params
24
- params.require(:confirmation).permit(:email)
25
- end
16
+ user.confirm! ? render_204 : render_422(user.errors)
17
+ end
26
18
 
27
- def create_error_response
28
- {errors: {email: [I18n.t('rails_jwt_auth.errors.not_found')]}}
29
- end
19
+ private
30
20
 
31
- def update_error_response(user)
32
- if user
33
- {errors: user.errors}
34
- else
35
- {errors: {confirmation_token: [I18n.t('rails_jwt_auth.errors.not_found')]}}
21
+ def confirmation_params
22
+ params.require(:confirmation).permit(:email)
36
23
  end
37
24
  end
38
25
  end
@@ -1,42 +1,29 @@
1
- class RailsJwtAuth::PasswordsController < ApplicationController
2
- def create
3
- user = RailsJwtAuth.model.where(email: create_password_params[:email]).first
4
- return render json: create_error_response, status: 422 unless user
1
+ module RailsJwtAuth
2
+ class PasswordsController < ApplicationController
3
+ include RenderHelper
5
4
 
6
- user.send_reset_password_instructions
7
- render json: {}, status: 204
8
- end
9
-
10
- def update
11
- user = RailsJwtAuth.model.where(reset_password_token: params[:reset_password_token]).first
12
- return render json: update_error_response(nil), status: 422 unless user
5
+ def create
6
+ user = RailsJwtAuth.model.where(email: create_password_params[:email]).first
7
+ return render_422(email: [I18n.t('rails_jwt_auth.errors.not_found')]) unless user
13
8
 
14
- if user.update_attributes(update_password_params)
15
- render json: {}, status: 204
16
- else
17
- render json: update_error_response(user), status: 422
9
+ user.send_reset_password_instructions ? render_204 : render_422(user.errors)
18
10
  end
19
- end
20
11
 
21
- private
12
+ def update
13
+ user = RailsJwtAuth.model.where(reset_password_token: params[:reset_password_token]).first
14
+ return render_422(reset_password_token: [I18n.t('rails_jwt_auth.errors.not_found')]) unless user
22
15
 
23
- def create_password_params
24
- params.require(:password).permit(:email)
25
- end
16
+ user.update_attributes(update_password_params) ? render_204 : render_422(user.errors)
17
+ end
26
18
 
27
- def create_error_response
28
- {errors: {email: [I18n.t('rails_jwt_auth.errors.not_found')]}}
29
- end
19
+ private
30
20
 
31
- def update_password_params
32
- params.require(:password).permit(:password, :password_confirmation)
33
- end
21
+ def create_password_params
22
+ params.require(:password).permit(:email)
23
+ end
34
24
 
35
- def update_error_response(user)
36
- if user
37
- {errors: user.errors}
38
- else
39
- {errors: {reset_password_token: [I18n.t('rails_jwt_auth.errors.not_found')]}}
25
+ def update_password_params
26
+ params.require(:password).permit(:password, :password_confirmation)
40
27
  end
41
28
  end
42
29
  end
@@ -1,35 +1,19 @@
1
- class RailsJwtAuth::RegistrationsController < ApplicationController
2
- def create
3
- user = RailsJwtAuth.model.new(create_params)
1
+ module RailsJwtAuth
2
+ class RegistrationsController < ApplicationController
3
+ include RenderHelper
4
4
 
5
- if user.save
6
- render json: create_success_response(user), status: 201
7
- else
8
- render json: create_error_response(user), status: 422
9
- end
10
- end
11
-
12
- private
13
-
14
- def root
15
- RailsJwtAuth.model_name.underscore
16
- end
5
+ def create
6
+ user = RailsJwtAuth.model.new(create_params)
17
7
 
18
- def create_success_response(user)
19
- {
20
- root => {
21
- id: user.id.to_s,
22
- RailsJwtAuth.auth_field_name => user.send(RailsJwtAuth.auth_field_name)
23
- }
24
- }
25
- end
8
+ user.save ? render_201(user) : render_422(user.errors)
9
+ end
26
10
 
27
- def create_error_response(user)
28
- {errors: user.errors}
29
- end
11
+ private
30
12
 
31
- def create_params
32
- params.require(root).permit(
33
- RailsJwtAuth.auth_field_name, :password, :password_confirmation)
13
+ def create_params
14
+ params.require(RailsJwtAuth.model_name.underscore).permit(
15
+ RailsJwtAuth.auth_field_name, :password, :password_confirmation
16
+ )
17
+ end
34
18
  end
35
19
  end
@@ -3,19 +3,21 @@ require 'rails_jwt_auth/jwt/request'
3
3
 
4
4
  module RailsJwtAuth
5
5
  class SessionsController < ApplicationController
6
+ include RenderHelper
7
+
6
8
  def create
7
9
  user = RailsJwtAuth.model.where(
8
10
  RailsJwtAuth.auth_field_name => create_params[RailsJwtAuth.auth_field_name].to_s.downcase
9
11
  ).first
10
12
 
11
13
  if !user
12
- render json: create_error_response(user), status: 422
14
+ render_422 session: [create_session_error]
13
15
  elsif user.respond_to?('confirmed?') && !user.confirmed?
14
- render json: unconfirmed_error_response, status: 422
16
+ render_422 session: [I18n.t('rails_jwt_auth.errors.unconfirmed')]
15
17
  elsif user.authenticate(create_params[:password])
16
- render json: create_success_response(user, get_jwt(user)), status: 201
18
+ render_201 session: {jwt: get_jwt(user)}
17
19
  else
18
- render json: create_error_response(user), status: 422
20
+ render_422 session: [create_session_error]
19
21
  end
20
22
  end
21
23
 
@@ -35,16 +37,8 @@ module RailsJwtAuth
35
37
  params.require(:session).permit(RailsJwtAuth.auth_field_name, :password)
36
38
  end
37
39
 
38
- def create_error_response(_user)
39
- {errors: {session: "Invalid #{RailsJwtAuth.auth_field_name} / password"}}
40
- end
41
-
42
- def unconfirmed_error_response
43
- {errors: {session: 'Unconfirmed email'}}
44
- end
45
-
46
- def create_success_response(_user, jwt)
47
- {session: {jwt: jwt}}
40
+ def create_session_error
41
+ I18n.t('rails_jwt_auth.errors.create_session', field: RailsJwtAuth.auth_field_name)
48
42
  end
49
43
  end
50
44
  end
@@ -0,0 +1,15 @@
1
+ module RailsJwtAuth
2
+ module RenderHelper
3
+ def render_201(resource)
4
+ render json: resource, root: true, status: 201
5
+ end
6
+
7
+ def render_204
8
+ render json: {}, status: 204
9
+ end
10
+
11
+ def render_422(errors)
12
+ render json: {errors: errors}, status: 422
13
+ end
14
+ end
15
+ end
@@ -1,7 +1,10 @@
1
1
  module RailsJwtAuth
2
2
  module Confirmable
3
3
  def send_confirmation_instructions
4
- return false if confirmed?
4
+ if confirmed?
5
+ errors.add(:email, I18n.t('rails_jwt_auth.errors.already_confirmed'))
6
+ return false
7
+ end
5
8
 
6
9
  self.confirmation_token = SecureRandom.base58(24)
7
10
  self.confirmation_sent_at = Time.now
@@ -6,7 +6,9 @@ en:
6
6
  reset_password_instructions.subject:
7
7
  subject: "Reset password instructions"
8
8
  errors:
9
+ unconfirmed: "unconfirmed email"
9
10
  already_confirmed: "was already confirmed, please try signing in"
11
+ create_session: "invalid %{field} / password"
10
12
  expired: "has expired, please request a new one"
11
13
  invalid: "invalid"
12
14
  not_found: "not found"
@@ -27,8 +27,8 @@ RailsJwtAuth.setup do |config|
27
27
  #config.confirmation_expiration_time = 1.day
28
28
 
29
29
  # url used to create email link with reset password token
30
- #config.reset_password_url
30
+ #config.reset_password_url = 'http://frontend.com/reset_password'
31
31
 
32
32
  # expiration time for reset password tokens
33
- #config.reset_password_expiration_time
33
+ #config.reset_password_expiration_time = 1.day
34
34
  end
@@ -1,3 +1,3 @@
1
1
  module RailsJwtAuth
2
- VERSION = '0.5.0'
2
+ VERSION = '0.6.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_jwt_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - rjurado
@@ -82,6 +82,7 @@ files:
82
82
  - app/controllers/rails_jwt_auth/registrations_controller.rb
83
83
  - app/controllers/rails_jwt_auth/sessions_controller.rb
84
84
  - app/controllers/unauthorized_controller.rb
85
+ - app/helpers/rails_jwt_auth/render_helper.rb
85
86
  - app/helpers/rails_jwt_auth/warden_helper.rb
86
87
  - app/mailers/rails_jwt_auth/mailer.rb
87
88
  - app/models/concerns/rails_jwt_auth/authenticatable.rb