Merb-auth quick tip

Posted by Guillaume Maury
on Dec 08, 08

How to keep things in the session object even after logging in

If you look at the source code from the merb-auth-slice-password sessions controller you have the following before filters

 before :_maintain_auth_session_before, :exclude => [:destroy]  # Need to hang onto the redirection during the session.abandon!
  before :_abandon_session,     :only => [:update, :destroy]
  before  :_maintain_auth_session_after,  :exclude => [:destroy]  # Need to hang onto the redirection during the session.abandon!

and the associated code:

  # @private
  def _maintain_auth_session_before
    @_maintain_auth_session = {}
    Merb::Authentication.maintain_session_keys.each do |k|
      @_maintain_auth_session[k] = session[k]
    end
  end
  
  # @private
  def _maintain_auth_session_after
    @_maintain_auth_session.each do |k,v|
      session[k] = v
    end
  end
  
  # @private
  def _abandon_session
    session.abandon!
  end

So if you want to keep some elements from the session accross login you just need to add the keys to Merb::Authentication.maintain_session_keys

Like this: (I put it in merb/merb-auth/setup.rb)

	Merb::Authentication.maintain_session_keys << :lang << :guest

I’m currently available for hire on a contract basis.

blog comments powered by Disqus