Skip to content
This repository was archived by the owner on Apr 14, 2018. It is now read-only.
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'

.env

# Ignore bundler config.
/.bundle

Expand Down
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ git_source(:github) do |repo_name|
"https://github.com/#{repo_name}.git"
end

gem "omniauth"
gem "omniauth-github"
gem "awesome_print"

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.2'
# Use postgresql as the database for Active Record
Expand Down Expand Up @@ -48,6 +52,8 @@ group :development, :test do

# Use pry for rails console
gem 'pry-rails'

gem 'dotenv-rails'
end

group :test do
Expand Down
32 changes: 31 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ GEM
tzinfo (~> 1.1)
ansi (1.5.0)
arel (7.1.4)
awesome_print (1.8.0)
babel-source (5.8.35)
babel-transpiler (0.7.0)
babel-source (>= 4.0, < 6)
Expand All @@ -60,15 +61,22 @@ GEM
execjs
coffee-script-source (1.12.2)
concurrent-ruby (1.0.5)
dotenv (2.2.1)
dotenv-rails (2.2.1)
dotenv (= 2.2.1)
railties (>= 3.2, < 5.2)
erubis (2.7.0)
execjs (2.7.0)
faraday (0.12.2)
multipart-post (>= 1.2, < 3)
ffi (1.9.18)
foundation-rails (6.3.0.0)
railties (>= 3.1.0)
sass (>= 3.3.0, < 3.5)
sprockets-es6 (>= 0.9.0)
globalid (0.3.7)
activesupport (>= 4.1.0)
hashie (3.5.6)
i18n (0.8.1)
jbuilder (2.6.3)
activesupport (>= 3.0.0, < 5.2)
Expand All @@ -77,6 +85,7 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
jwt (1.5.6)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand Down Expand Up @@ -104,9 +113,26 @@ GEM
minitest (~> 5.0)
rails (>= 4.1)
multi_json (1.12.1)
multi_xml (0.6.0)
multipart-post (2.0.0)
nio4r (2.0.0)
nokogiri (1.7.1)
mini_portile2 (~> 2.1.0)
oauth2 (1.4.0)
faraday (>= 0.8, < 0.13)
jwt (~> 1.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
omniauth (1.7.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.4.0)
oauth2 (~> 1.0)
omniauth (~> 1.2)
pg (0.20.0)
pry (0.10.4)
coderay (~> 1.1.0)
Expand Down Expand Up @@ -193,9 +219,11 @@ PLATFORMS
ruby

DEPENDENCIES
awesome_print
better_errors
byebug
coffee-rails (~> 4.2)
dotenv-rails
foundation-rails
jbuilder (~> 2.5)
jquery-rails
Expand All @@ -204,6 +232,8 @@ DEPENDENCIES
minitest-reporters
minitest-skip
minitest-spec-rails
omniauth
omniauth-github
pg (~> 0.18)
pry-rails
puma (~> 3.0)
Expand All @@ -220,4 +250,4 @@ RUBY VERSION
ruby 2.4.0p0

BUNDLED WITH
1.14.4
1.15.3
24 changes: 24 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,27 @@ main {
padding-top: 2rem;
padding-bottom: 2rem;
}

.github-login {
display: block;
margin: auto;
text-align: center;
}

.divider {
border-top: 1px solid #d9dadc;
display: block;
line-height: 1px;
margin: 15px auto ;
position: relative;
text-align: center;
width: 80%;
}

.divider .divider-title {
background: #fff;
font-size: 12px;
letter-spacing: 1px;
padding: 0 20px;
text-transform: uppercase;
}
33 changes: 28 additions & 5 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,39 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

before_action :find_user
before_action :require_login, except: [:root]

def render_404
# DPR: supposedly this will actually render a 404 page in production
raise ActionController::RoutingError.new('Not Found')
end

private
def find_user
if session[:user_id]
@login_user = User.find_by(id: session[:user_id])
private

def find_user
if session[:user_id]
@login_user = User.find_by(id: session[:user_id])
end
end
end

def logged_in?
!@login_user.nil?
end

def require_login
unless logged_in?
flash[:status] = :failure
flash[:result_text] = "You must log in to do that"

redirect_to root_path
end
end

# def authenticate
# unless session[:user_id]
# flash[:status] = :failure
# flash[:result_text] = "You must log in to do that"
# redirect_to root_path
# end
# end
end
51 changes: 51 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class SessionsController < ApplicationController
skip_before_action :require_login


def login_form
end

Expand Down Expand Up @@ -31,4 +34,52 @@ def logout
flash[:result_text] = "Successfully logged out"
redirect_to root_path
end

def create
@auth_hash = request.env['omniauth.auth']
# ap @auth_hash

@user = User.find_by(uid: @auth_hash['uid'], provider: @auth_hash['provider'])

if @user
session[:user_id] = @user.id #earlier, mistyped as @user_id??
flash[:success] = "Successfully logged in as #{@user.username}"
else
# @user = User.new uid: @auth_hash['uid'], provider: @auth_hash['provider'], username: @auth_hash['info']['nickname'], email: @auth_hash['info']['email']
#
# if @user.save
# session[:user_id] = @user.id
# flash[:success] = "Welcome #{@user.username}"
# else
# flash[:error] = "Unable to save user"
# end

@user = User.build_from_github(@auth_hash)

if @user
session[:user_id] = @user.id
flash[:success] = "Welcome #{@user.username}"
else
flash[:error] = "Unable to save user"
end
end

redirect_to root_path

# auth_hash = request.env['omniauth.auth']
#
# if auth_hash['uid']
# @user = User.find_by(uid: auth_hash[:uid], provider: 'github')
# if @user.nil?
# # User doesn't match anything in the DB
# # Attempt to create a new user
# else
# flash[:success] = "Logged in successfully"
# redirect_to root_path
# end
# else
# flash[:error] = "Could not log in"
# redirect_to root_path
# end
end
end
16 changes: 16 additions & 0 deletions app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class WorksController < ApplicationController
# of work we're dealing with
before_action :category_from_work, except: [:root, :index, :new, :create]

before_action :require_work_owner, only: [:update, :destroy]

def root
@albums = Work.best_albums
@books = Work.best_books
Expand All @@ -20,6 +22,7 @@ def new

def create
@work = Work.new(media_params)
@work.user_id = @login_user.id
@media_category = @work.category
if @work.save
flash[:status] = :success
Expand Down Expand Up @@ -98,4 +101,17 @@ def category_from_work
render_404 unless @work
@media_category = @work.category.downcase.pluralize
end

def work_owner?
@login_user == User.find_by(id: @work.user_id)
end

def require_work_owner
unless work_owner?
flash[:status] = :failure
flash[:result_text] = "You must be the owner of this work to do that"

redirect_to root_path
end
end
end
10 changes: 10 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
class User < ApplicationRecord
has_many :votes
has_many :ranked_works, through: :votes, source: :work
has_many :works

validates :username, uniqueness: true, presence: true
validates :uid, presence: true
validates :provider, presence: true

def self.build_from_github(auth_hash)
user = User.new uid: auth_hash['uid'], provider: auth_hash['provider'], username: auth_hash['info']['nickname'], email: auth_hash['info']['email']

user.save ? user : nil

end
end
2 changes: 2 additions & 0 deletions app/models/work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class Work < ApplicationRecord
has_many :votes, dependent: :destroy
has_many :ranking_users, through: :votes, source: :user

belongs_to :user

validates :category, presence: true,
inclusion: { in: CATEGORIES }

Expand Down
4 changes: 2 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
<%= link_to "View all users", users_path, class: "button" %>
<div class="float-right">
<% if @login_user %>
<%= link_to "Logged in as #{@login_user.username}", user_path(@login_user), class: "button" %>
<!-- <%= link_to "Logged in as #{@login_user.username}", user_path(@login_user), class: "button" %> -->
<%= link_to "Log Out", logout_path, method: :post, class: "button" %>
<% else %>
<%= link_to "Log In", login_path, class: "button float-right" %>
<%= link_to "Log In with Github", "/auth/github", class: "button float-right" %>
<% end %>
</div>
</nav>
Expand Down
13 changes: 13 additions & 0 deletions app/views/sessions/login_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
<h2>Log In</h2>

<%= form_tag do %>
<%= label_tag(:username, "Username") %>
<%= text_field_tag(:username) %>
<%= submit_tag("Log In", class: "button") %>

<% end %>

<div class = "divider">
<strong class= "divider-title">or</strong>
</div>


<section class = "github-login">
<%= link_to "Log In with Github", "auth/github", class: "button" %>
</section>

<hr />

<article>
<h4>A note about logging in</h4>
<p>
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
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
post '/login', to: 'sessions#login'
post '/logout', to: 'sessions#logout', as: 'logout'

get "/auth/:provider/callback", to: "sessions#create", as: 'auth_callback'

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

Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20171016202649_add_ui_dand_providerto_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddUiDandProvidertoUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :uid, :integer
add_column :users, :provider, :string
add_column :users, :email, :string
end
end
6 changes: 6 additions & 0 deletions db/migrate/20171017144432_add_user_to_works.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddUserToWorks < ActiveRecord::Migration[5.0]
def change
add_column :works, :user_id, :integer
add_foreign_key :works, :users
end
end
Loading