Devise Session Limit

2019-08-18 17:24发布

问题:

I have a Rails app running Rails 3.2.6 and Devise 2.1.2 and I want to be able to limit concurrent logins to 1. I've tried using the Devise Security Extension but keep getting an undefined method for unique_session_id.

Instead I want to see if I can override Devise's behavior as in this SO article: devise limit one session per user at a time

I understand how it works and I've generated a sessions controller, but I'm not sure how to force the app to use it as an override for Devise. Also in the controller should I be inheriting from the Application Controller or the Devise Controller?

Everything in place right now however the app doesn't write a login_token which tells me that I'm not using the controller properly or something else might be amiss.

Any help is appreciated.

回答1:

I would strongly recommend you check out the Devise Wiki. Chances are when doing something like this, the answer can be found there. Your new controller will need to inherit from the Devise Sessions Controller as such:

class MySessionsController < Devise::SessionsController

This way, any methods you did not specifically override will still be called, thus not breaking other functionality.

In order to make sure that Devise uses your controller instead of it's own, make your devise_for call in routes.rb look as follows:

devise_for :users, :controllers => { :sessions => "my_sessions" }

From there Devise will pick up your controller. It's probably a good idea to restart your server in there just to be sure.