Skip to content

Instantly share code, notes, and snippets.

@rogercampos
Created February 6, 2013 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogercampos/4721805 to your computer and use it in GitHub Desktop.
Save rogercampos/4721805 to your computer and use it in GitHub Desktop.
Example of how to use sorcery to authenticate outside the controller scope
module Sorcery
class FreeAuth
include Controller
def initialize(request)
@request = request
end
def session
@session ||= @request.session
end
def cookies
@cookies ||= ActionDispatch::Cookies::CookieJar.build(@request)
end
end
def self.with_request(request)
yield(FreeAuth.new(request)) if block_given?
end
end
class CanCanConstraint
def initialize(action, context)
@action, @context = action, context
end
def matches?(request)
Sorcery.with_request(request) do |c|
c.current_user && Ability.new(c.current_user).can?(@action, @context)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment