Sha256: 0138c9f16f111f8fc3792e89fce799e2a2c3853a73b8f8f6f12208c3cd9c136b

Contents?: true

Size: 1.53 KB

Versions: 10

Compression:

Stored size: 1.53 KB

Contents

# frozen-string-literal: true

module Rodauth
  SessionExpiration = Feature.define(:session_expiration) do
    error_flash "This session has expired, please login again."

    auth_value_method :max_session_lifetime, 86400
    auth_value_method :session_created_session_key, :session_created_at
    auth_value_method :session_expiration_default, true
    auth_value_method :session_inactivity_timeout, 1800
    auth_value_method :session_last_activity_session_key, :last_session_activity_at

    auth_value_methods :session_expiration_redirect
    
    def check_session_expiration
      return unless logged_in?

      unless session.has_key?(session_last_activity_session_key) && session.has_key?(session_created_session_key)
        if session_expiration_default
          expire_session
        end

        return
      end

      time = Time.now.to_i

      if session[session_last_activity_session_key] + session_inactivity_timeout < time
        expire_session
      end
      set_session_value(session_last_activity_session_key, time)

      if session[session_created_session_key] + max_session_lifetime < time
        expire_session
      end
    end

    def expire_session
      clear_session
      set_redirect_error_flash session_expiration_error_flash
      redirect session_expiration_redirect
    end

    def session_expiration_redirect
      require_login_redirect
    end

    private

    def update_session
      super
      session[session_last_activity_session_key] = session[session_created_session_key] = Time.now.to_i
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rodauth-1.9.0 lib/rodauth/features/session_expiration.rb
rodauth-1.8.0 lib/rodauth/features/session_expiration.rb
rodauth-1.7.0 lib/rodauth/features/session_expiration.rb
rodauth-1.6.0 lib/rodauth/features/session_expiration.rb
rodauth-1.5.0 lib/rodauth/features/session_expiration.rb
rodauth-1.4.0 lib/rodauth/features/session_expiration.rb
rodauth-1.3.0 lib/rodauth/features/session_expiration.rb
rodauth-1.2.0 lib/rodauth/features/session_expiration.rb
rodauth-1.1.0 lib/rodauth/features/session_expiration.rb
rodauth-1.0.0 lib/rodauth/features/session_expiration.rb