Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6556461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:56:51+00:00 2026-05-25T12:56:51+00:00

I have a scenario where i need to restrict users from having only one

  • 0

I have a scenario where i need to restrict users from having only one active session at a time. Mine is a rails3 application and uses devise for authentication. I’m interested in keeping only the latest user session active. i.e., if a user logs in while there is another session active for the same username, i want to inactivate the older session and allow the recent one. Is there a way, in devise, to get hold of user sessions and invalidate them?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:56:52+00:00Added an answer on May 25, 2026 at 12:56 pm

    You can track a particular user’s session by storing a unique token specific to that user in the database.

    Create a migration to add the field for storing the token. I assume the devise model is User.

    class AddSignInTokenToUsers < ActiveRecord::Migration
      def change
        add_column :users, :current_sign_in_token, :string
      end
    end
    

    Add the following code to application_controller.rb

    class ApplicationController < ActionController::Base
      before_filter :invalidate_simultaneous_user_session, :unless => Proc.new {|c| c.controller_name == 'sessions' and c.action_name == 'create' }
    
      def invalidate_simultaneous_user_session
        sign_out_and_redirect(current_user) if current_user && session[:sign_in_token] != current_user.current_sign_in_token
      end
    
      def sign_in(resource_or_scope, *args)
        super
        token = Devise.friendly_token
        current_user.update_attribute :current_sign_in_token, token
        session[:sign_in_token] = token
      end
    end
    

    sign_in(resource_or_scope, *args) is a devise hook that will be called every time the user logs in.

    invalidate_simultaneous_user_session will log out the current user if another instance of the current user logs in. This will ensure that only one session is active for a user at any instance of time.

    invalidate_simultaneous_user_session filter should be skipped for the user login action to update the newly logged in user’s token. I am not happy with using a Proc to skip the filter based on controller name and action. If you have already overridden devise’s sessions_controller, then include skip_before_filter :check_simultaneous_user_session inside that controller and you can get rid of the Proc!

    Hope this helps..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scenario where I need to upload a file from one web
I have a scenario where I need to search from many binary files (using
I have a scenario where I need to pull the title from a img
I have a scenario where I need to supply users a message. The message
I have the following scenario.I need to recommend artists to users. How should this
I have a scenario where I need to show a bunch of buttons only
I have Problem with chat application . my scenario: I need to build chat
I have this scenario where I need data integrity in the physical database. For
I have a scenario where I need to do the following in a transaction:
I have a scenario where I need a desktop console app to communicate with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.