Module: Monban::ControllerHelpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/monban/controller_helpers.rb

Overview

Mixin to be included in Rails controllers.

Since:

  • 0.0.15

Instance Method Summary collapse

Instance Method Details

#authenticate(user, password) ⇒ User?

Note:

Uses the Services::Authentication service to verify the user’s credentials

Authenticates a user given a password

Parameters:

  • user (User)

    the user

  • password (String)

    the password

Returns:

  • (User)

    if authentication succeeded

  • (nil)

    if authentication failed

Since:

  • 0.0.15



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?

Note:

Uses the Services::Authentication service to verify the user’s details

Authenticates a session.

Examples:

Basic usage

class SessionsController < ApplicationController
  def create
    user = authenticate_session(session_params)

     if (user)
       redirect_to(root_path)
     else
       render :new
     end
   end

   private

   def session_params
     params.require(:session).permit(:email, :password)
   end

 end

Using the field map to authenticate using multiple lookup fields

class SessionsController < ApplicationController
  def create
    user = authenticate_session(session_params, email_or_username: [:email, :username])

     if (user)
       redirect_to(root_path)
     else
       render :new
     end
   end

   private

   def session_params
     params.require(:session).permit(:email_or_username, :password)
   end

 end

Parameters:

  • session_params (Hash)

    params containing lookup and token fields

  • field_map (Hash) (defaults to: nil)

    Field map used for allowing users to sign in with multiple fields e.g. email and username

Returns:

  • (User)

    if authentication succeeded

  • (nil)

    if authentication failed

Since:

  • 0.0.15



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_userUser?

helper_method that returns the current user

Returns:

  • (User)

    if user is signed in

  • (nil)

    if user is not signed in

Since:

  • 0.0.15



138
139
140
# File 'lib/monban/controller_helpers.rb', line 138

def current_user
  @current_user ||= warden.user
end

#require_loginObject

Note:

Uses the no login handler

before_action that determines what to do when the user is not signed in

Since:

  • 0.0.15



153
154
155
156
157
# File 'lib/monban/controller_helpers.rb', line 153

def 
  unless signed_in?
    Monban.config..call(self)
  end
end

#reset_password(user, password) ⇒ Object

Note:

Uses the Services::PasswordReset service to change a user’s password

Resets a user’s password

Parameters:

  • user (User)

    the user

  • password (String)

    the password

Since:

  • 0.0.15



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

Note:

Uses the Services::SignIn service to create a session

Sign in a user

Parameters:

  • user (User)

    the user object to sign in

Yields:

  • Yields to the block if the user is successfully signed in

Returns:

  • (Object)

    returns the value from calling perform on the Services::SignIn service

Since:

  • 0.0.15



20
21
22
23
24
25
26
# File 'lib/monban/controller_helpers.rb', line 20

def  user
  Monban.config..new(user, warden).perform.tap do |status|
    if status && block_given?
      yield
    end
  end
end

#sign_outObject

Note:

Uses the Services::SignOut service to destroy the session

Sign out the current session

Returns:

  • (Object)

    returns the value from calling perform on the Services::SignOut service

Since:

  • 0.0.15



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

Note:

Uses the Services::SignUp service to create a user

Sign up a user

Parameters:

  • user_params (Hash)

    params containing lookup and token fields

Yields:

  • Yields to the block if the user is signed up successfully

Returns:

  • (Object)

    returns the value from calling perform on the Services::SignUp service

Since:

  • 0.0.15



44
45
46
47
48
49
50
# File 'lib/monban/controller_helpers.rb', line 44

def  user_params
  Monban.config..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

Returns:

  • (User)

    if user is signed in

  • (nil)

    if user is not signed in

Since:

  • 0.0.15



146
147
148
# File 'lib/monban/controller_helpers.rb', line 146

def signed_in?
  warden.user
end

#wardenObject

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.

Since:

  • 0.0.15



130
131
132
# File 'lib/monban/controller_helpers.rb', line 130

def warden
  request.env['warden']
end