Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub oauth #72

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ gem 'omniauth-google-oauth2'
gem 'will_paginate'
gem 'acts-as-taggable-on', '~> 6.0'
gem 'omniauth-census', git: "https://github.com/turingschool-projects/omniauth-census"

gem 'omniauth-github'

group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
Expand All @@ -43,6 +43,7 @@ group :development, :test do
gem 'vcr'
gem 'selenium-webdriver'
gem 'chromedriver-helper'
gem 'rubocop', require: false
end

group :development do
Expand Down
24 changes: 23 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ GEM
archive-zip (0.11.0)
io-like (~> 0.3.0)
arel (9.0.0)
ast (2.4.0)
awesome_print (1.8.0)
babel-source (5.8.35)
babel-transpiler (0.7.0)
Expand Down Expand Up @@ -143,6 +144,7 @@ GEM
i18n (1.1.0)
concurrent-ruby (~> 1.0)
io-like (0.3.0)
jaro_winkler (1.5.2)
jbuilder (2.7.0)
activesupport (>= 4.2.0)
multi_json (>= 1.2)
Expand Down Expand Up @@ -188,6 +190,9 @@ GEM
omniauth (1.8.1)
hashie (>= 3.4.6, < 3.6.0)
rack (>= 1.6.2, < 3)
omniauth-github (1.3.0)
omniauth (~> 1.5)
omniauth-oauth2 (>= 1.4.0, < 2.0)
omniauth-google-oauth2 (0.5.3)
jwt (>= 1.5)
omniauth (>= 1.1.1)
Expand All @@ -196,10 +201,14 @@ GEM
oauth2 (~> 1.1)
omniauth (~> 1.2)
os (1.0.0)
parallel (1.14.0)
parser (2.6.0.0)
ast (~> 2.4.0)
pg (1.0.0)
pry (0.11.3)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
psych (3.1.0)
public_suffix (3.0.3)
puma (3.12.0)
rack (2.0.5)
Expand Down Expand Up @@ -231,6 +240,7 @@ GEM
method_source
rake (>= 0.8.7)
thor (>= 0.19.0, < 2.0)
rainbow (3.0.0)
rake (12.3.1)
rb-fsevent (0.10.3)
rb-inotify (0.9.10)
Expand All @@ -257,6 +267,15 @@ GEM
rspec-mocks (~> 3.8.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)
rubocop (0.66.0)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
parser (>= 2.5, != 2.5.1.1)
psych (>= 3.1.0)
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 1.6)
ruby-progressbar (1.10.0)
ruby_dep (1.5.0)
rubyzip (1.2.1)
safe_yaml (1.0.4)
Expand Down Expand Up @@ -299,6 +318,7 @@ GEM
tzinfo (1.2.5)
thread_safe (~> 0.1)
uber (0.1.0)
unicode-display_width (1.5.0)
vcr (4.0.0)
web-console (3.6.2)
actionview (>= 5.0)
Expand Down Expand Up @@ -347,12 +367,14 @@ DEPENDENCIES
launchy
listen (>= 3.0.5, < 3.2)
omniauth-census!
omniauth-github
omniauth-google-oauth2
pg (>= 0.18, < 2.0)
pry
puma (~> 3.11)
rails (~> 5.2.0)
rspec-rails
rubocop
sass-rails (~> 5.0)
selenium-webdriver
shoulda-matchers
Expand All @@ -369,4 +391,4 @@ RUBY VERSION
ruby 2.4.1p111

BUNDLED WITH
1.16.2
2.0.1
13 changes: 13 additions & 0 deletions app/controllers/github/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Github::SessionsController < ApplicationController

def create
user = User.from_omniauth(request.env["omniauth.auth"], current_user)
if user
session[:user_id] = current_user.id
redirect_to dashboard_path
end
end



end
3 changes: 3 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class UsersController < ApplicationController
def show
render locals: {
facade: DashboardFacade.new({quantity: 5}, current_user)
}
end

def new
Expand Down
33 changes: 33 additions & 0 deletions app/facades/dashboard_facade.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class DashboardFacade

def initialize(attributes, current_user = nil)
@quantity = attributes[:quantity] || 0
@current_user = current_user
end

def repositories
response = service.get_repos(@quantity, @current_user)
response.map do |repo_data|
Repository.new(repo_data)
end
end

def followers
response = service.get_followers(@current_user)
response.map do |follower_data|
GithubUser.new(follower_data)
end
end

def following
response = service.get_following(@current_user)
response.map do |following_data|
GithubUser.new(following_data)
end
end

def service
GithubService.new
end

end
8 changes: 8 additions & 0 deletions app/models/github_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class GithubUser
attr_reader :handle

def initialize(attributes)
@attributes = attributes
@handle = attributes[:login]
end
end
11 changes: 11 additions & 0 deletions app/models/repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Repository
attr_reader :name,
:full_name

def initialize(attributes)
@attributes = attributes
@name = attributes[:name]
@full_name = attributes[:full_name]
end

end
9 changes: 8 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ class User < ApplicationRecord
has_many :videos, through: :user_videos

validates :email, uniqueness: true, presence: true
validates_presence_of :password
validates_presence_of :first_name
enum role: [:default, :admin]
has_secure_password

def self.from_omniauth(auth_info, current_user)

current_user.update(uid: auth_info.uid,
handle: auth_info.extra.raw_info.login,
access_token: auth_info.credentials.token)
end

end
28 changes: 28 additions & 0 deletions app/services/github_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class GithubService

def get_repos(quantity = 0, current_user)
repos = get_json('/user/repos', current_user)
range = (quantity - 1)
repos[0..range]
end

def get_followers(current_user)
get_json('/user/followers', current_user)
end

def get_following(current_user)
get_json('/user/following', current_user)
end

def get_json(url, current_user)
response = conn(current_user).get(url)
JSON.parse(response.body, symbolize_names: true)
end

def conn(current_user)
Faraday.new(url: "https://api.github.com") do |faraday|
faraday.headers["Authorization"] = "token #{current_user.access_token}"
faraday.adapter Faraday.default_adapter
end
end
end
31 changes: 31 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,35 @@
<section>
<h1>Bookmarked Segments</h1>
</section>

<%= button_to "Connect to Github", github_connect_path %>

<% if current_user.access_token %>
<section id="Github">
<h1>Github</h1>
<section id="repositories">
<% facade.repositories.each do |repo| %>
<ul class="repository">
<li class="name"><%= link_to repo.name, "https://github.com/#{repo.full_name}" %></li>
</ul>
<% end %>
</section>
<section id="followers">
<h2>Followers</h2>
<% facade.followers.each do |follower| %>
<ul class="follower">
<li class="handle"><%= link_to follower.handle, "https://github.com/#{follower.handle}" %></li>
</ul>
<% end %>
</section>
<section id="following">
<h2>Following</h2>
<% facade.following.each do |user| %>
<ul class="user">
<li class="handle"><%= link_to user.handle, "https://github.com/#{user.handle}" %></li>
</ul>
<% end %>
</section>
</section>
<% end %>
</section>
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
config.webpacker.check_yarn_integrity = false

end
1 change: 1 addition & 0 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET']
provider :github, ENV['GITHUB_CLIENT_ID'], ENV['GITHUB_CLIENT_SECRET']
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
delete '/logout', to: "sessions#destroy"

get '/dashboard', to: 'users#show'
get '/auth/github', as: :github_connect
get '/auth/github/callback', to: 'github/sessions#create'
get '/about', to: 'about#show'
get '/get_started', to: 'get_started#show'

Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20190321173225_add_uid_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUidToUsers < ActiveRecord::Migration[5.2]
def change
add_column :users, :uid, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20190321193106_add_token_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddTokenToUsers < ActiveRecord::Migration[5.2]
def change
add_column :users, :access_token, :string
end
end
5 changes: 5 additions & 0 deletions db/migrate/20190321194551_addhandleto_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddhandletoUser < ActiveRecord::Migration[5.2]
def change
add_column :users, :handle, :string
end
end
5 changes: 4 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_07_31_230036) do
ActiveRecord::Schema.define(version: 2019_03_21_194551) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -67,6 +67,9 @@
t.integer "role", default: 0
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "uid"
t.string "access_token"
t.string "handle"
t.index ["email"], name: "index_users_on_email"
end

Expand Down
Loading