Module: Authlogic::Session::Foundation::InstanceMethods

Defined in:
lib/authlogic/session/foundation.rb

Constant Summary collapse

E_AC_PARAMETERS =
"Passing an ActionController::Parameters to Authlogic is not allowed.\n\nIn Authlogic 3, especially during the transition of rails to Strong\nParameters, it was common for Authlogic users to forget to `permit`\ntheir params. They would pass their params into Authlogic, we'd call\n`to_h`, and they'd be surprised when authentication failed.\n\nIn 2018, people are still making this mistake. We'd like to help them\nand make authlogic a little simpler at the same time, so in Authlogic\n3.7.0, we deprecated the use of ActionController::Parameters. Instead,\npass a plain Hash. Please replace:\n\n    UserSession.new(user_session_params)\n    UserSession.create(user_session_params)\n\nwith\n\n    UserSession.new(user_session_params.to_h)\n    UserSession.create(user_session_params.to_h)\n\nAnd don't forget to `permit`!\n\nWe discussed this issue thoroughly between late 2016 and early\n2018. Notable discussions include:\n\n- https://github.com/binarylogic/authlogic/issues/512\n- https://github.com/binarylogic/authlogic/pull/558\n- https://github.com/binarylogic/authlogic/pull/577\n".freeze

Instance Method Summary collapse

Instance Method Details

#credentialsObject

The credentials you passed to create your session. See credentials= for more info.



52
53
54
# File 'lib/authlogic/session/foundation.rb', line 52

def credentials
  []
end

#credentials=(values) ⇒ Object

Set your credentials before you save your session. There are many method signatures.

“‘ # A hash of credentials is most common session.credentials = { login: “foo”, password: “bar”, remember_me: true }

# You must pass an actual Hash, ActionController::Parameters is # specifically not allowed.

# You can pass an array of objects: session.credentials = [my_user_object, true]

# If you need to set an id (see Authlogic::Session::Id) pass it # last. It needs be the last item in the array you pass, since the id # is something that you control yourself, it should never be set from # a hash or a form. Examples: session.credentials = [

{:login => "foo", :password => "bar", :remember_me => true},
:my_id

] session.credentials = [my_user_object, true, :my_id]

# Finally, there’s priority_record

{ priority_record: my_object }, :my_id

“‘



82
83
84
85
86
87
# File 'lib/authlogic/session/foundation.rb', line 82

def credentials=(values)
  normalized = Array.wrap(values)
  if normalized.first.class.name == "ActionController::Parameters"
    raise TypeError.new(E_AC_PARAMETERS)
  end
end

#initialize(*args) ⇒ Object



46
47
48
# File 'lib/authlogic/session/foundation.rb', line 46

def initialize(*args)
  self.credentials = args
end

#inspectObject



89
90
91
92
93
94
95
# File 'lib/authlogic/session/foundation.rb', line 89

def inspect
  format(
    "#<%s: %s>",
    self.class.name,
    credentials.blank? ? "no credentials provided" : credentials.inspect
  )
end