Module: Oath::ControllerHelpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/oath/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



117
118
119
# File 'lib/oath/controller_helpers.rb', line 117

def authenticate user, password
  Oath.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



101
102
103
104
105
106
107
# File 'lib/oath/controller_helpers.rb', line 101

def authenticate_session session_params, field_map = nil
  token_field = Oath.config.user_token_field
  params_hash = Oath.transform_params(session_params).symbolize_keys
  password = params_hash.fetch(token_field)
  user = Oath.lookup(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



140
141
142
# File 'lib/oath/controller_helpers.rb', line 140

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



155
156
157
158
159
# File 'lib/oath/controller_helpers.rb', line 155

def 
  unless signed_in?
    Oath.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



127
128
129
# File 'lib/oath/controller_helpers.rb', line 127

def reset_password user, password
  Oath.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



22
23
24
25
26
27
28
# File 'lib/oath/controller_helpers.rb', line 22

def  user
  Oath.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



35
36
37
# File 'lib/oath/controller_helpers.rb', line 35

def sign_out
  Oath.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



46
47
48
49
50
51
52
# File 'lib/oath/controller_helpers.rb', line 46

def  user_params
  Oath.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



148
149
150
# File 'lib/oath/controller_helpers.rb', line 148

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



132
133
134
# File 'lib/oath/controller_helpers.rb', line 132

def warden
  request.env['warden']
end