Module: Authgasm::Session::Config::ClassMethods

Defined in:
lib/authgasm/session/config.rb

Overview

Config

Configuration is simple. The configuration options are just class methods. Just put this in your config/initializers directory

UserSession.configure do |config|
  config.authenticate_with = User
  # ... more configuration
end

or you can set your configuration in the session class directly:

class UserSession < Authgasm::Session::Base
  self.authenticate_with = User
  # ... more configuration
end

See the methods belows for all configuration options.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

The name of the cookie or the key in the cookies hash. Be sure and use a unique name. If you have multiple sessions and they use the same cookie it will cause problems. Also, if a id is set it will be inserted into the beginning of the string. Exmaple:

session = UserSession.new(:super_high_secret)
session.cookie_key => "super_high_secret_user_credentials"
  • Default: “#Authgasm::Session::Config::ClassMethods.klass_nameklass_name.underscore_credentials”

  • Accepts: String



58
59
60
# File 'lib/authgasm/session/config.rb', line 58

def cookie_key
  @cookie_key ||= "#{klass_name.underscore}_credentials"
end

The authentication credentials are stored in a cookie as: user##cookie_separatorcrypted_password.

  • Default: “:::”

  • Accepts: String



45
46
47
# File 'lib/authgasm/session/config.rb', line 45

def cookie_separator
  @cookie_separator ||= ":::"
end

#find_by_login_methodObject

The name of the method used to find the record by the login. What’s nifty about this is that you can do anything in your method, Authgasm will just pass you the login.

Let’s say you allow users to login by username or email. Set this to “find_login”, or whatever method you want. Then in your model create a class method like:

def self.()
  () || find_by_email()
end
  • Default: “find_by_##login_field

  • Accepts: Symbol or String



73
74
75
# File 'lib/authgasm/session/config.rb', line 73

def 
  @find_by_login_method ||= "find_by_#{}"
end

#find_withObject

Calling UserSession.find tries to find the user session by session, then cookie, then basic http auth. This option allows you to change the order or remove any of these.

  • Default: [:session, :cookie, :http_auth]

  • Accepts: Array, and can only use any of the 3 options above



82
83
84
# File 'lib/authgasm/session/config.rb', line 82

def find_with
  @find_with ||= [:session, :cookie, :http_auth]
end

#login_fieldObject

The name of the method you want Authgasm to create for storing the login / username. Keep in mind this is just for your Authgasm::Session, if you want it can be something completely different than the field in your model. So if you wanted people to login with a field called “login” and then find users by email this is compeltely doable. See the find_by_login_method configuration option for more details.

  • Default: Guesses based on the model columns, tries login, username, and email. If none are present it defaults to login

  • Accepts: Symbol or String



93
94
95
# File 'lib/authgasm/session/config.rb', line 93

def 
  @login_field ||= (klass.column_names.include?("login") && :login) || (klass.column_names.include?("username") && :username) || (klass.column_names.include?("email") && :email) || :login
end

#password_fieldObject

Works exactly like login_field, but for the password instead.

  • Default: Guesses based on the model columns, tries password and pass. If none are present it defaults to password

  • Accepts: Symbol or String



102
103
104
# File 'lib/authgasm/session/config.rb', line 102

def password_field
  @password_field ||= (klass.column_names.include?("password") && :password) || (klass.column_names.include?("pass") && :pass) || :password
end

#remember_meObject

If sessions should be remembered by default or not.

  • Default: false

  • Accepts: Boolean



111
112
113
# File 'lib/authgasm/session/config.rb', line 111

def remember_me
  @remember_me
end

#remember_token_fieldObject

The name of the field that the remember token is stored. This is for cookies. Let’s say you set up your app and want all users to be remembered for 6 months. Then you realize that might be a little too long. Well they already have a cookie set to expire in 6 months. Without a token you would have to reset their password, which obviously isn’t feasible. So instead of messing with their password just reset their remember token. Next time they access the site and try to login via a cookie it will be rejected and they will have to relogin.

  • Default: Guesses based on the model columns, tries remember_token, remember_key, cookie_token, and cookie_key. If none are present it defaults to remember_token

  • Accepts: Symbol or String



136
137
138
139
140
141
142
143
# File 'lib/authgasm/session/config.rb', line 136

def remember_token_field
  @remember_token_field ||=
    (klass.column_names.include?("remember_token") && :remember_token) ||
    (klass.column_names.include?("remember_key") && :remember_key) ||
    (klass.column_names.include?("cookie_token") && :cookie_token) ||
    (klass.column_names.include?("cookie_key") && :cookie_key) ||
    :remember_token
end

#session_keyObject

Works exactly like cookie_key, but for sessions. See cookie_key for more info.

  • Default: cookie_key

  • Accepts: Symbol or String



150
151
152
# File 'lib/authgasm/session/config.rb', line 150

def session_key
  @session_key ||= cookie_key
end

#verify_password_methodObject

The name of the method in your model used to verify the password. This should be an instance method. It should also be prepared to accept a raw password and a crytped password.



159
160
161
# File 'lib/authgasm/session/config.rb', line 159

def verify_password_method
  @verify_password_method ||= "valid_#{password_field}?"
end

Instance Method Details

#authenticate_with=(klass) ⇒ Object

Lets you change which model to use for authentication.

  • Default: inferred from the class name. UserSession would automatically try User

  • Accepts: an ActiveRecord class



31
32
33
34
# File 'lib/authgasm/session/config.rb', line 31

def authenticate_with=(klass)
  @klass_name = klass.name
  @klass = klass
end

#configure {|_self| ... } ⇒ Object

Convenience method that lets you easily set configuration, see examples above

Yields:

  • (_self)

Yield Parameters:



37
38
39
# File 'lib/authgasm/session/config.rb', line 37

def configure
  yield self
end

#remember_me_forObject

The length of time until the cookie expires.

  • Default: 3.months

  • Accepts: Integer, length of time in seconds, such as 60 or 3.months



120
121
122
123
# File 'lib/authgasm/session/config.rb', line 120

def remember_me_for
  return @remember_me_for if @set_remember_me_for
  @remember_me_for ||= 3.months
end

#remember_me_for=(value) ⇒ Object

:nodoc:



125
126
127
128
# File 'lib/authgasm/session/config.rb', line 125

def remember_me_for=(value)  # :nodoc:
  @set_remember_me_for = true
  @remember_me_for = value
end