Module: Sinatra::Doorman::RememberMe

Defined in:
lib/doorman/base.rb

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/doorman/base.rb', line 153

def self.registered(app)
  app.use Rack::Cookies

  Warden::Strategies.add(:remember_me, RememberMeStrategy)

  app.before do
    env['warden'].authenticate(:remember_me)
  end

  Warden::Manager.after_authentication do |user, auth, opts|
    if auth.winning_strategy.is_a?(RememberMeStrategy) ||
      (auth.winning_strategy.is_a?(PasswordStrategy) &&
         auth.params['user']['remember_me'])
      user.remember_me!  # new token
      auth.env['rack.cookies'][COOKIE_KEY] = { 
        :value => user.remember_token, 
        :expires => Time.now + 7 * 24 * 3600, 
        :path => '/' }
    end
  end

  Warden::Manager.before_logout do |user, auth, opts|
    user.forget_me! if user
    auth.env['rack.cookies'].delete(COOKIE_KEY)
  end
end