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

Jackie's MediaRanker Oauth and tests #42

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
# Ignore bundler config.
/.bundle

# Ignore git credentials file
.env

# Ignore simple cov generate files in the coverage directory
coverage/*

# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
Expand Down
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ gem 'jbuilder', '~> 2.5'
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem "omniauth"
gem "omniauth-github"

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
Expand All @@ -53,6 +55,7 @@ group :test do
gem 'minitest-reporters'
gem 'minitest-spec-rails'
gem 'minitest-skip'
gem 'simplecov', require: false
end

group :development do
Expand All @@ -62,6 +65,7 @@ group :development do
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'dotenv-rails'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
Expand Down
36 changes: 36 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,19 @@ GEM
concurrent-ruby (1.0.5)
crass (1.0.4)
debug_inspector (0.0.3)
docile (1.3.1)
dotenv (2.5.0)
dotenv-rails (2.5.0)
dotenv (= 2.5.0)
railties (>= 3.2, < 6.0)
erubi (1.7.1)
execjs (2.7.0)
faraday (0.15.3)
multipart-post (>= 1.2, < 3)
ffi (1.9.25)
globalid (0.4.1)
activesupport (>= 4.2.0)
hashie (3.5.7)
i18n (1.1.0)
concurrent-ruby (~> 1.0)
jbuilder (2.7.0)
Expand All @@ -84,6 +92,8 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (2.1.0)
jwt (2.1.0)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand Down Expand Up @@ -113,9 +123,26 @@ GEM
minitest (~> 5.0)
rails (>= 4.1)
multi_json (1.13.1)
multi_xml (0.6.0)
multipart-post (2.0.0)
nio4r (2.3.1)
nokogiri (1.8.4)
mini_portile2 (~> 2.3.0)
oauth2 (1.4.1)
faraday (>= 0.8, < 0.16.0)
jwt (>= 1.0, < 3.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
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-oauth2 (1.5.0)
oauth2 (~> 1.1)
omniauth (~> 1.2)
pg (0.21.0)
popper_js (1.14.3)
pry (0.11.3)
Expand Down Expand Up @@ -167,6 +194,11 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
simplecov (0.16.1)
docile (~> 1.1)
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.2)
spring (2.0.2)
activesupport (>= 4.2)
spring-watcher-listen (2.0.1)
Expand Down Expand Up @@ -207,18 +239,22 @@ DEPENDENCIES
bootstrap (~> 4.1.3)
byebug
coffee-rails (~> 4.2)
dotenv-rails
jbuilder (~> 2.5)
jquery-rails
listen (~> 3.0.5)
minitest-rails
minitest-reporters
minitest-skip
minitest-spec-rails
omniauth
omniauth-github
pg (~> 0.18)
pry-rails
puma (~> 3.0)
rails
sass-rails
simplecov
spring
spring-watcher-listen (~> 2.0.0)
turbolinks (~> 5)
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

before_action :find_user
before_action :require_login

def render_404
# DPR: this will actually render a 404 page in production
Expand All @@ -14,4 +15,11 @@ def find_user
@login_user = User.find_by(id: session[:user_id])
end
end

def require_login
if @login_user.nil?
flash[:warning] = "You must be logged in to view this section"
redirect_to root_path
end
end
end
37 changes: 18 additions & 19 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
class SessionsController < ApplicationController
def login_form
end
skip_before_action :require_login, only: [:create]

def login
username = params[:username]
if username and user = User.find_by(username: username)
session[:user_id] = user.id
flash[:status] = :success
flash[:result_text] = "Successfully logged in as existing user #{user.username}"
def create
auth_hash = request.env['omniauth.auth']
user = User.find_by(uid: auth_hash[:uid], provider: 'github')
if user
flash[:success] = "Logged in as returning user #{user.username}"
else
user = User.new(username: username)
if user.save
session[:user_id] = user.id
flash[:status] = :success
flash[:result_text] = "Successfully created new user #{user.username} with ID #{user.id}"
user = User.build_from_github(auth_hash)
user.save
if user.username
flash[:success] = "Created user #{user.username}"
else
flash.now[:status] = :failure
flash.now[:result_text] = "Could not log in"
flash.now[:messages] = user.errors.messages
render "login_form", status: :bad_request
flash[:warning] = "Could not create user account."
redirect_to root_path
return
end
end

session[:user_id] = user.id
redirect_to root_path
end

def logout
session[:user_id] = nil
flash[:status] = :success
flash[:result_text] = "Successfully logged out"
flash[:success] = "Successfully logged out!"


redirect_to root_path
end

end
14 changes: 4 additions & 10 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class WorksController < ApplicationController
# We should always be able to tell what category
# of work we're dealing with
before_action :category_from_work, except: [:root, :index, :new, :create]
skip_before_action :require_login, only: [:root]

def root
@albums = Work.best_albums
Expand Down Expand Up @@ -50,7 +51,7 @@ def update
flash.now[:status] = :failure
flash.now[:result_text] = "Could not update #{@media_category.singularize}"
flash.now[:messages] = @work.errors.messages
render :edit, status: :not_found
render :edit, status: :bad_request
end
end

Expand All @@ -63,21 +64,14 @@ def destroy

def upvote
flash[:status] = :failure
if @login_user
vote = Vote.new(user: @login_user, work: @work)
if vote.save
if vote.save
flash[:status] = :success
flash[:result_text] = "Successfully upvoted!"
else
else
flash[:result_text] = "Could not upvote"
flash[:messages] = vote.errors.messages
end
else
flash[:result_text] = "You must log in to do that"
end

# Refresh the page to show either the updated vote count
# or the error message
redirect_back fallback_location: work_path(@work)
end

Expand Down
9 changes: 9 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,13 @@ class User < ApplicationRecord
has_many :ranked_works, through: :votes, source: :work

validates :username, uniqueness: true, presence: true


def self.build_from_github(auth_hash)
user = User.new
user.provider = "github"
user.username = auth_hash[:info][:nickname]
user.uid = auth_hash[:uid]
return user
end
end
11 changes: 9 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
<%= link_to "Logged in as #{@login_user.username}", user_path(@login_user), class: "btn btn-primary" %>
</li>
<li class="nav-item app-header__nav_item">
<%= link_to "Log Out", logout_path, method: :post, class: "btn btn-primary" %>
<%= link_to "Log Out", logout_path, method: "delete", class: "btn btn-primary" %>
</li>

<% else %>

<li class="nav-item app-header__nav_item">
<%= link_to "Log In", login_path, class: "btn btn-primary" %>
<%= link_to "Login with Github", "/auth/github", class: "btn btn-primary" %>
</li>
<% end %>

Expand All @@ -68,6 +68,13 @@
<% end %>
</div>
</section>
<% else %>
<% flash.each do |name, message| %>
<section class="alert-<%= name %> flashmessage">
<p><%= message %> </p>
<% end %>
</section>

<% end %>

<main>
Expand Down
3 changes: 3 additions & 0 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Rails.application.config.middleware.use OmniAuth::Builder do
provider :github, ENV["GITHUB_CLIENT_ID"], ENV["GITHUB_CLIENT_SECRET"], scope: "user:email"
end
7 changes: 5 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root 'works#root'
get '/login', to: 'sessions#login_form', as: 'login'
post '/login', to: 'sessions#login'
post '/logout', to: 'sessions#logout', as: 'logout'
# post '/login', to: 'sessions#login'
# post '/logout', to: 'sessions#logout', as: 'logout'

resources :works
post '/works/:id/upvote', to: 'works#upvote', as: 'upvote'

resources :users, only: [:index, :show]

get "/auth/:provider/callback", to: "sessions#create", as: 'auth_callback'
delete "/logout", to: 'sessions#logout', as: "logout"
end
5 changes: 5 additions & 0 deletions db/migrate/20181016211710_add_columns_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddColumnsToUser < ActiveRecord::Migration[5.2]
def change
add_column :users, :uid, :integer, null: false
end
end
5 changes: 5 additions & 0 deletions db/migrate/20181016212826_add_more_columns_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddMoreColumnsToUser < ActiveRecord::Migration[5.2]
def change
add_column :users, :provider, :string, null: false
end
end
36 changes: 19 additions & 17 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,37 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170407164321) do
ActiveRecord::Schema.define(version: 2018_10_16_212826) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "users", force: :cascade do |t|
t.string "username"
create_table "users", id: :serial, force: :cascade do |t|
t.string "username"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "uid", null: false
t.string "provider", null: false
end

create_table "votes", force: :cascade do |t|
t.integer "user_id"
t.integer "work_id"
create_table "votes", id: :serial, force: :cascade do |t|
t.integer "user_id"
t.integer "work_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_votes_on_user_id", using: :btree
t.index ["work_id"], name: "index_votes_on_work_id", using: :btree
t.index ["user_id"], name: "index_votes_on_user_id"
t.index ["work_id"], name: "index_votes_on_work_id"
end

create_table "works", force: :cascade do |t|
t.string "title"
t.string "creator"
t.string "description"
t.string "category"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "vote_count", default: 0
t.integer "publication_year"
create_table "works", id: :serial, force: :cascade do |t|
t.string "title"
t.string "creator"
t.string "description"
t.string "category"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "vote_count", default: 0
t.integer "publication_year"
end

add_foreign_key "votes", "users"
Expand Down
Loading