lib/sorcery/providers/github.rb in sorcery-0.9.0 vs lib/sorcery/providers/github.rb in sorcery-0.9.1

- old
+ new

@@ -24,11 +24,13 @@ def get_user_hash(access_token) response = access_token.get(user_info_path) auth_hash(access_token).tap do |h| - h[:user_info] = JSON.parse(response.body) + h[:user_info] = JSON.parse(response.body).tap do |uih| + uih['email'] = primary_email(access_token) if scope =~ /user/ + end h[:uid] = h[:user_info]['id'] end end # calculates and returns the url to which the user should be redirected, @@ -42,9 +44,16 @@ args = {}.tap do |a| a[:code] = params[:code] if params[:code] end get_access_token(args, token_url: token_url, token_method: :post) + end + + def primary_email(access_token) + response = access_token.get(user_info_path + "/emails") + emails = JSON.parse(response.body) + primary = emails.find{|i| i['primary'] } + primary && primary['email'] || emails.first && emails.first['email'] end end end end