Search code examples
ruby-on-railsauthenticationdevise

automatic logout after inactivity/idle


How to set up in the rails application that if any user is idle for 30 minutes or a specific period of time he should be automatically get logged out. Can any one give any solution . i am using devise for authentication purpose. Any help is appreciated .


Solution

  • You should use Timeoutable model trait.

    Timeoutable takes care of veryfing whether a user session has already expired or not. When a session expires after the configured time, the user will be asked for credentials again, it means, he/she will be redirected to the sign in page.

    Options

    Timeoutable adds the following options to devise_for:

    • +timeout_in+: the interval to timeout the user session without activity.

    In your model you need

    devise :timeoutable
    # along with :database_authenticatable, :registerable and other things.
    

    Also, take a look at config/initializers/devise.rb, you can configure timeout value there.