Skip to content

Instantly share code, notes, and snippets.

@capoferro
Created March 16, 2011 04:51
Show Gist options
  • Save capoferro/872035 to your computer and use it in GitHub Desktop.
Save capoferro/872035 to your computer and use it in GitHub Desktop.
Github OAuth with automatic account creation with AuthLogic
def create
url = 'https://github.com/login/oauth/access_token'
query = 'client_id=' + Github.config[:client_id] +
'&redirect_uri=' + Github.config[:redirect_uri] +
'&client_secret=' + Github.config[:secret] +
'&code=' + params[:code]
token_response = Curl::Easy.http_post(url, query).body_str
if token_response.empty?
flash[:notice] = 'GitHub didn\'t respond. Try again?'
redirect_to login_path and return
end
token = token_response.split('=')[1]
url = 'https://github.com/api/v2/json/user/show?access_token=' + token
data = JSON::parse(Curl::Easy.http_get(url).body_str)['user']
@user = User.where(email: data['email']).first
# Login if the user exists
unless @user.nil?
@user_session = UserSession.create(@user)
redirect_back_or_default(root_url)
return
end
@user = User.new(login: data['login'], email: data['email'], github_access_token: token)
if @user.save
redirect_back_or_default root_url
else
render :action => :new
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment