Created
March 23, 2016 00:46
-
-
Save StasKoval/a6e3081d0c4dd9250115 to your computer and use it in GitHub Desktop.
This file contains 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 AuthenticateUser | |
attr_accessor :user | |
def initialize(authorization_code) | |
@authorization_code = authorization_code | |
end | |
def perform | |
token = app_client.exchange_code_for_token(@authorization_code) | |
user_data = user_client(token).user | |
if user_data.email.blank? | |
user_data.email = user_client.emails.find { |e| e.primary }.email | |
end | |
self.user = User.find_or_create_from_oauth(user_data) | |
end | |
def success? | |
user.present? && user.persisted? | |
end | |
private | |
# for 3rd party client | |
# def app_client | |
# @app_client ||= Octokit::Client.new( | |
# client_id: ENV['GITHUB_CLIENT_ID'], | |
# client_secret: ENV['GITHUB_CLIENT_SECRET'] | |
# ) | |
# end | |
def user_client(token) | |
Octokit::Client.new(access_token: token.access_token) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment