Skip to content

Instantly share code, notes, and snippets.

@ch1c0t
Created December 24, 2016 11:17
Show Gist options
  • Save ch1c0t/a307589db70f8b161234b745349510e6 to your computer and use it in GitHub Desktop.
Save ch1c0t/a307589db70f8b161234b745349510e6 to your computer and use it in GitHub Desktop.
module Hobby
module Auth
def self.[] *users
Module.new do
define_singleton_method :included do |app|
users.each do |user|
app.define_singleton_method user.downcase do |route|
action = route.action
route.action = -> do
if user = (user.find_by_token request.headers['Authorization'])
instance_exec &action
else
response.status = 403
end
end
end
end
end
end
end
end
end # Ends should be optional. Block nesting can be infered from two-space indent.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment