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
115 116 117 |
# File 'lib/monban/controller_helpers.rb', line 115 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.
99 100 101 102 103 104 105 |
# File 'lib/monban/controller_helpers.rb', line 99 def authenticate_session session_params, field_map = nil token_field = Monban.config.user_token_field session_params_hash = session_params.to_h.symbolize_keys password = session_params_hash.fetch(token_field) user = Monban.lookup(session_params_hash.except(token_field), field_map) authenticate(user, password) end |
#current_user ⇒ User?
helper_method that returns the current user
138 139 140 |
# File 'lib/monban/controller_helpers.rb', line 138 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
153 154 155 156 157 |
# File 'lib/monban/controller_helpers.rb', line 153 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
125 126 127 |
# File 'lib/monban/controller_helpers.rb', line 125 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
20 21 22 23 24 25 26 |
# File 'lib/monban/controller_helpers.rb', line 20 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
33 34 35 |
# File 'lib/monban/controller_helpers.rb', line 33 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
44 45 46 47 48 49 50 |
# File 'lib/monban/controller_helpers.rb', line 44 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
146 147 148 |
# File 'lib/monban/controller_helpers.rb', line 146 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.
130 131 132 |
# File 'lib/monban/controller_helpers.rb', line 130 def warden request.env['warden'] end |