Skip to content

Instantly share code, notes, and snippets.

@TechFounder
Last active December 27, 2015 04:29
Show Gist options
  • Save TechFounder/7266742 to your computer and use it in GitHub Desktop.
Save TechFounder/7266742 to your computer and use it in GitHub Desktop.
If you need to make sure that only one Devise user class is logged in and that users can't register as another class if they are already logged in, you need to pre-check their status. To do that use these following snippets.
# Create this new controller for registrations to check that they're not already registered
class RegistrationsController < Devise::RegistrationsController
def new
if (user_signed_in? || admin_signed_in?)
redirect_to '/', alert: "You cannot register two roles at the same time!"
else
super
end
end
end
# Put :controllers => {:sessions => "sessions", :registrations => "registrations"} after all devise_for
YourApp::Application.routes.draw do
devise_for :users, :controllers => {:sessions => "sessions", :registrations => "registrations"}
devise_for :admins, :controllers => {:sessions => "sessions", :registrations => "registrations"}
end
# Create this new controller for sessions to check that they're not already logged in
class SessionsController < Devise::SessionsController
def new
if (user_signed_in? || admin_signed_in?)
redirect_to '/', alert: "You can only sign into a single user!"
else
super
end
end
end
@TechFounder
Copy link
Author

You can also redirect to :back instead of '/'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment