Skip to content
This repository has been archived by the owner on Oct 16, 2019. It is now read-only.

Commit

Permalink
Persist membership cache between requests
Browse files Browse the repository at this point in the history
The membership cache was being serialized into the user object. It was
assumed that the user object was serialized at the end of each request.
It turns out that this is not the case. Instead, the user object only
gets serialized once, namely after setting the user. This means that the
membership cache wasn't working at all between requests.

This commit fixes it by not including the memberships in the serialized
user as there's no point in doing so. Instead, after the user object is
set up, the memberships hash get retrieved from the user session (warden
provides a session object for each scope; if there is none, a new hash
is stored in the session) and assigned to the user object. The hash is
then used as before and we get the session serialization from rack for
free.

Fixes #52.
  • Loading branch information
fphilipe committed May 13, 2017
1 parent 59c4ce7 commit 63d3549
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/warden/github/hook.rb
Expand Up @@ -4,3 +4,10 @@

strategy.finalize_flow! if strategy.class == Warden::GitHub::Strategy
end

Warden::Manager.after_set_user do |user, auth, opts|
if user.is_a?(Warden::GitHub::User)
session = auth.session(opts.fetch(:scope))
user.memberships = session[:_memberships] ||= {}
end
end
4 changes: 3 additions & 1 deletion lib/warden/github/user.rb
@@ -1,8 +1,10 @@
module Warden
module GitHub
class User < Struct.new(:attribs, :token, :browser_session_id, :memberships)
class User < Struct.new(:attribs, :token, :browser_session_id)
ATTRIBUTES = %w[id login name gravatar_id avatar_url email company site_admin].freeze

attr_accessor :memberships

def self.load(access_token, browser_session_id = nil)
api = Octokit::Client.new(:access_token => access_token)
data = { }
Expand Down

0 comments on commit 63d3549

Please sign in to comment.