Last active
December 27, 2015 04:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can also redirect to :back instead of '/'