Created
December 2, 2011 21:00
-
-
Save scottwater/1424816 to your computer and use it in GitHub Desktop.
Possible Authenticated Constraint for Sorcery
This file contains hidden or 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
# SCOPE GETS CROSSED - DO NOT USE AS IS! | |
class AuthenticatedConstraint | |
extend Sorcery::Controller::InstanceMethods | |
def self.matches?(request) | |
Sorcery::Controller::InstanceMethods.send(:define_method, :session, proc{request.session}) | |
Sorcery::Controller::InstanceMethods.send(:define_method, :request, proc{request}) | |
logged_in? | |
end | |
end |
This file contains hidden or 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
constraints(AuthenticatedConstraint) do | |
root :to => 'home#index' | |
end |
This file contains hidden or 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
class AuthenticatedConstraint | |
def self.matches?(request) | |
!!request.session[:user_id] | |
end | |
end |
Thanx for the feedback.
Ok, maybe I'm dumb, but this doesn't work here :(
Scope gets crossed.
(Updating this thread in case others stumble across it later)
I think either going with a stripped-down version of Devise (which is built atop Warden) is a good option, or just roll your own atop Warden.
I'm about to cross this bridge (needing to share authentication w/a mounted app & needing to constrain routes based on authentication) for an app currently using Sorcery and will like rip it out in favor of the stripped-down Devise.
Here is my solution to using constraints with Sorcery. I really did not want to switch to Devise after investing in Sorcery. NoamB/sorcery#341 (comment)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think @scottwater is correct - this will work. However, I'd rather see Sorcery push this down to the Rack level so that the authentication can be used by other engines that may be mounted, and which are unaware of Rails/ActiveRecord. See: NoamB/sorcery#155