Module: Monban::ControllerHelpers
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/monban/controller_helpers.rb
Overview
Mixin to be included in Rails controllers.
Instance Method Summary collapse
-
#authenticate(user, password) ⇒ User?
Authenticates a user given a password.
-
#authenticate_session(session_params, field_map = nil) ⇒ User?
Authenticates a session.
-
#current_user ⇒ User?
helper_method that returns the current user.
-
#require_login ⇒ Object
before_action that determines what to do when the user is not signed in.
-
#reset_password(user, password) ⇒ Object
Resets a user’s password.
-
#sign_in(user) { ... } ⇒ Object
Sign in a user.
-
#sign_out ⇒ Object
Sign out the current session.
-
#sign_up(user_params) { ... } ⇒ Object
Sign up a user.
-
#signed_in? ⇒ User?
helper_method that checks if there is a user signed in.
- #warden ⇒ Object private
Instance Method Details
#authenticate(user, password) ⇒ User?
Uses the Services::Authentication service to verify the user’s credentials
Authenticates a user given a password
117 118 119 |
# File 'lib/monban/controller_helpers.rb', line 117 def authenticate user, password Monban.config.authentication_service.new(user, password).perform end |
#authenticate_session(session_params, field_map = nil) ⇒ User?
Uses the Services::Authentication service to verify the user’s details
Authenticates a session.
101 102 103 104 105 106 107 |
# File 'lib/monban/controller_helpers.rb', line 101 def authenticate_session session_params, field_map = nil token_field = Monban.config.user_token_field params_hash = Monban.transform_params(session_params).symbolize_keys password = params_hash.fetch(token_field) user = Monban.lookup(params_hash.except(token_field), field_map) authenticate(user, password) end |
#current_user ⇒ User?
helper_method that returns the current user
140 141 142 |
# File 'lib/monban/controller_helpers.rb', line 140 def current_user @current_user ||= warden.user end |
#require_login ⇒ Object
Uses the no login handler
before_action that determines what to do when the user is not signed in
155 156 157 158 159 |
# File 'lib/monban/controller_helpers.rb', line 155 def require_login unless signed_in? Monban.config.no_login_handler.call(self) end end |
#reset_password(user, password) ⇒ Object
Uses the Services::PasswordReset service to change a user’s password
Resets a user’s password
127 128 129 |
# File 'lib/monban/controller_helpers.rb', line 127 def reset_password user, password Monban.config.password_reset_service.new(user, password).perform end |
#sign_in(user) { ... } ⇒ Object
Uses the Services::SignIn service to create a session
Sign in a user
22 23 24 25 26 27 28 |
# File 'lib/monban/controller_helpers.rb', line 22 def sign_in user Monban.config.sign_in_service.new(user, warden).perform.tap do |status| if status && block_given? yield end end end |
#sign_out ⇒ Object
Uses the Services::SignOut service to destroy the session
Sign out the current session
35 36 37 |
# File 'lib/monban/controller_helpers.rb', line 35 def sign_out Monban.config.sign_out_service.new(warden).perform end |
#sign_up(user_params) { ... } ⇒ Object
Uses the Services::SignUp service to create a user
Sign up a user
46 47 48 49 50 51 52 |
# File 'lib/monban/controller_helpers.rb', line 46 def sign_up user_params Monban.config.sign_up_service.new(user_params).perform.tap do |status| if status && block_given? yield end end end |
#signed_in? ⇒ User?
helper_method that checks if there is a user signed in
148 149 150 |
# File 'lib/monban/controller_helpers.rb', line 148 def signed_in? warden.user end |
#warden ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
132 133 134 |
# File 'lib/monban/controller_helpers.rb', line 132 def warden request.env['warden'] end |