• Stars
    star
    124
  • Rank 288,207 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 15 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Provides automatic session timeout in a Rails application.

Build Status

Auto::Session::Timeout

Provides automatic session timeout in a Rails application. Very easy to install and configure. Have you ever wanted to force your users off your app if they go idle for a certain period of time? Many online banking sites use this technique. If your app is used on any kind of public computer system, this gem is a necessity.

Installation

Add this line to your application's Gemfile:

gem 'auto-session-timeout'

And then execute:

$ bundle

Or install it yourself as:

$ gem install auto-session-timeout

Usage

After installing, tell your application controller to use auto timeout:

class ApplicationController < ActionController::Base
  auto_session_timeout 1.hour
end

This will use a global timeout of 1 hour. The gem assumes your authentication provider has a #current_user method that returns the currently logged in user.

If you want to specify a custom timeout value per user, don't pass a value to the controller as shown above. Instead, override #auto_timeout in your #current_user model. This is typically the User class:

class ApplicationController < ActionController::Base
  auto_session_timeout
end

class User < ActiveRecord::Base
  def auto_timeout
    15.minutes
  end
end

You will also need to insert a call to the #auto_session_timeout_js helper method inside the body tags in your views. The easiest way to do this is to insert it once inside your default or application-wide layout. Make sure you are only rendering if the user is logged in, otherwise the gem will attempt to force non-existent sessions to timeout, wreaking havoc:

<body>
  <% if current_user %>
    <%= auto_session_timeout_js %>
  <% end %>
</body>

You need to setup two actions: one to return the session status and another that runs when the session times out. You can use the default actions included with the gem by inserting this line in your target controller (most likely your user or session controller):

class SessionsController < ApplicationController
  auto_session_timeout_actions
end

To customize the default actions, simply override them. You can call the #render_session_status and #render_session_timeout methods to use the default implementation from the gem, or you can define the actions entirely with your own custom code:

class SessionsController < ApplicationController
  def active
    render_session_status
  end

  def timeout
    render_session_timeout
  end
end

In any of these cases, make sure to properly map the actions in your routes.rb file:

get "active",  to: "sessions#active"
get "timeout", to: "sessions#timeout"

You're done! Enjoy watching your sessions automatically timeout.

Using with Devise

When using Devise for authentication you will need to add a scoped sessions controller and call the timeout actions helper there. For example:

class Users::SessionsController < Devise::SessionsController
  auto_session_timeout_actions
end

In your routes.rb file you will need to declare your scoped controller and declare the timeout actions inside the same Devise scope:

Rails.application.routes.draw do
  devise_for :users, controllers: { sessions: "users/sessions" }
  
  devise_scope :user do
    get "active", to: "users/sessions#active"
    get "timeout", to: "users/sessions#timeout"
  end
end

You can use Devise's #user_signed_in? method when you call the JS helper method in your view:

<body>
  <% if user_signed_in? %>
    <%= auto_session_timeout_js %>
  <% end %>
</body>

Optional Configuration

By default, the JavaScript code checks the server every 60 seconds for active sessions. If you prefer that it check more frequently, pass a frequency attribute to the helper method. The frequency is given in seconds. The following example checks the server every 15 seconds:

<body>
  <% if current_user %>
    <%= auto_session_timeout_js frequency: 15 %>
  <% end %>
</body>

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Resources

More Repositories

1

rspec_validation_expectations

Adds several handy expectations for testing ActiveRecord model validations and associations.
Ruby
40
star
2

textile_toolbar

Adds a handy toolbar to any Textile-enabled text area.
Ruby
30
star
3

uploadify

Adds multi-file upload support to your Rails application using Uploadify, a JQuery plugin.
Ruby
19
star
4

railspdf

Adds pdf-writer support to Rails. The original project is outdated. This fork supports 2.3.2 and adds a few other goodies.
Ruby
18
star
5

test_spec_on_rails

Rails plugin with helpers to test your app using test/spec.
Ruby
17
star
6

lindo

Adds a helper method to your controller and integration tests that automatically opens the response body in the default browser.
Ruby
10
star
7

asset_copier

Intelligent asset management for Rails plugins.
Ruby
7
star
8

radiant-spreedly-extension

Adds support for paid subscriptions through Spreedly.
Ruby
7
star
9

finder_filter

An easy way to add common finders to your Rails controllers.
Ruby
6
star
10

spread

Makes it easy to add tell-a-friend functionality to any Rails app.
Ruby
6
star
11

image_picker

A simple, customizable image browser.
JavaScript
4
star
12

octopress-muut

Use Muut for Octopress post and page comments.
Ruby
4
star
13

pass_nils_to

Pass nils to methods in various permutations. Expose bugs. Profit.
Ruby
2
star
14

youtube_tags

Enables inclusion of YouTube videos within Radiant pages.
Ruby
2
star
15

time-zone-warp

Warp time zones in your tests.
Ruby
2
star
16

ocadia_mephisto

Port of Becca Wei's excellent Ocadia theme for WordPress to Mephisto.
2
star
17

rtunes

Control iTunes remotely using Ruby OSA calls.
Ruby
1
star
18

patient-age

Calculates medical patient age in years, months, weeks and days.
Ruby
1
star
19

twitrosterize

Rails plugin that adds a helper method to easily generate Twit Rosters.
Ruby
1
star
20

radiant-textile_toolbar-extension

Adds toolbars to Textile-enabled text areas in the admin.
JavaScript
1
star