diff --git a/.gitignore b/.gitignore index 48fb168..a43d794 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,7 @@ # If you find yourself ignoring temporary files generated by your text editor # or operating system, you probably want to add a global ignore instead: # git config --global core.excludesfile '~/.gitignore_global' - +.env # Ignore bundler config. /.bundle diff --git a/Gemfile b/Gemfile index 24756e7..59f8cfc 100644 --- a/Gemfile +++ b/Gemfile @@ -7,6 +7,9 @@ git_source(:github) do |repo_name| "https://github.com/#{repo_name}.git" end +gem "omniauth" +gem "omniauth-github" + # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.0.2' # Use postgresql as the database for Active Record @@ -48,6 +51,7 @@ group :development, :test do # Use pry for rails console gem 'pry-rails' + gem 'dotenv-rails' end group :test do diff --git a/Gemfile.lock b/Gemfile.lock index 4d99ffe..abab408 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -60,8 +60,14 @@ 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) @@ -69,6 +75,7 @@ GEM 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) @@ -77,6 +84,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) @@ -104,9 +112,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) @@ -196,6 +221,7 @@ DEPENDENCIES better_errors byebug coffee-rails (~> 4.2) + dotenv-rails foundation-rails jbuilder (~> 2.5) jquery-rails @@ -204,6 +230,8 @@ DEPENDENCIES minitest-reporters minitest-skip minitest-spec-rails + omniauth + omniauth-github pg (~> 0.18) pry-rails puma (~> 3.0) @@ -220,4 +248,4 @@ RUBY VERSION ruby 2.4.0p0 BUNDLED WITH - 1.14.4 + 1.15.4 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 087352a..1b80452 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -12,6 +12,10 @@ def render_404 def find_user if session[:user_id] @login_user = User.find_by(id: session[:user_id]) + else + flash[:status] = :failure + flash[:result_text] = "Please log in to do that." + redirect_to root_path end end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 5bce99e..25dc122 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,34 +1,69 @@ class SessionsController < ApplicationController - def login_form - end + skip_before_action :find_user + + def create + @auth_hash = request.env['omniauth.auth'] + puts @auth_hash - def login - username = params[:username] - if username and user = User.find_by(username: username) - session[:user_id] = user.id + @user = User.find_by(uid: @auth_hash['uid'], provider: @auth_hash['provider']) + if @user + session[:user_id] = @user.id flash[:status] = :success - flash[:result_text] = "Successfully logged in as existing user #{user.username}" + flash[:result_text] = "Welcome back, #{@user.username}" + redirect_to root_path else - user = User.new(username: username) - if user.save - session[:user_id] = user.id + @user = User.new uid: @auth_hash['uid'], provider: @auth_hash['provider'], username: @auth_hash['info']['name'], email: @auth_hash['info']['email'] + if @user.save + session[:user_id] = @user.id flash[:status] = :success - flash[:result_text] = "Successfully created new user #{user.username} with ID #{user.id}" + flash[:result_text] = "Welcome, #{@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 - return + flash[:status] = :failure + flash[:result_text] = "Unable to save user!" end + redirect_to root_path end - redirect_to root_path end + def logout session[:user_id] = nil flash[:status] = :success flash[:result_text] = "Successfully logged out" redirect_to root_path end + + + def login_form + end + # + # 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}" + # 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}" + # 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 + # return + # end + # end + # redirect_to root_path + # end + # + # def logout + # session[:user_id] = nil + # flash[:status] = :success + # flash[:result_text] = "Successfully logged out" + # redirect_to root_path + # end end diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 1293d1d..db5e038 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -2,8 +2,12 @@ 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 :find_user, only: [:root] def root + if session[:user_id] + @login_user = User.find_by(id: session[:user_id]) + end @albums = Work.best_albums @books = Work.best_books @movies = Work.best_movies @@ -20,6 +24,7 @@ def new def create @work = Work.new(media_params) + @work.user = @login_user @media_category = @work.category if @work.save flash[:status] = :success @@ -38,6 +43,11 @@ def show end def edit + if @work.user != @login_user + flash[:status] = :failure + flash[:result_text] = "Log in to do this, please." + redirect_to root_path + end end def update @@ -55,6 +65,11 @@ def update end def destroy + if @work.user != @login_user + flash[:status] = :failure + flash[:result_text] = "This isn't yours to destroy." + redirect_to root_path + end @work.destroy flash[:status] = :success flash[:result_text] = "Successfully destroyed #{@media_category.singularize} #{@work.id}" diff --git a/app/models/user.rb b/app/models/user.rb index 4cac8fe..6f7a5fd 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,9 @@ class User < ApplicationRecord has_many :votes has_many :ranked_works, through: :votes, source: :work + has_many :works validates :username, uniqueness: true, presence: true + + end diff --git a/app/models/work.rb b/app/models/work.rb index 2fd3e66..a639cc4 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -2,6 +2,7 @@ class Work < ApplicationRecord CATEGORIES = %w(album book movie) has_many :votes, dependent: :destroy has_many :ranking_users, through: :votes, source: :user + belongs_to :user, dependent: :destroy validates :category, presence: true, inclusion: { in: CATEGORIES } diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 0180f1b..face997 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -18,16 +18,18 @@ diff --git a/app/views/sessions/create.html.erb b/app/views/sessions/create.html.erb new file mode 100644 index 0000000..e69de29 diff --git a/app/views/works/show.html.erb b/app/views/works/show.html.erb index 1c7ef59..b8d44f1 100644 --- a/app/views/works/show.html.erb +++ b/app/views/works/show.html.erb @@ -5,9 +5,11 @@

<%= @work.description %>

<%= link_to "Back to media ranks", root_path, class: "button" %> - <%= link_to "Edit", edit_work_path(@work), class: "button" %> <%= link_to "Upvote", upvote_path(@work), class: "button", method: :post %> - <%= link_to "Delete", work_path(@work), class: "alert button", method: "delete", data: { confirm: "Are you sure?" } %> + <% if @login_user == @work.user && @work.user != nil %> + <%= link_to "Edit", edit_work_path(@work), class: "button" %> + <%= link_to "Delete", work_path(@work), class: "alert button", method: "delete", data: { confirm: "Are you sure?" } %> + <% end %>
diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb new file mode 100644 index 0000000..fd44161 --- /dev/null +++ b/config/initializers/omniauth.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index a7e8af1..b2839c1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,4 +9,6 @@ post '/works/:id/upvote', to: 'works#upvote', as: 'upvote' resources :users, only: [:index, :show] + + get "/auth/:provider/callback", to: "sessions#create" end diff --git a/db/migrate/20171016203112_update_users.rb b/db/migrate/20171016203112_update_users.rb new file mode 100644 index 0000000..bfa8df8 --- /dev/null +++ b/db/migrate/20171016203112_update_users.rb @@ -0,0 +1,10 @@ +class UpdateUsers < ActiveRecord::Migration[5.0] + def change + remove_column :users, :uid, :integer + remove_column :users, :email, :string + remove_column :users, :provider, :string + add_column :users, :uid, :integer, null: false + add_column :users, :email, :string + add_column :users, :provider, :string, null: false + end +end diff --git a/db/migrate/20171030050556_add_user_to_works.rb b/db/migrate/20171030050556_add_user_to_works.rb new file mode 100644 index 0000000..daaed64 --- /dev/null +++ b/db/migrate/20171030050556_add_user_to_works.rb @@ -0,0 +1,5 @@ +class AddUserToWorks < ActiveRecord::Migration[5.0] + def change + add_reference :works, :user, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 6bc8ba5..608b545 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170407164321) do +ActiveRecord::Schema.define(version: 20171030050556) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -19,6 +19,9 @@ t.string "username" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "uid", null: false + t.string "email" + t.string "provider", null: false end create_table "votes", force: :cascade do |t| @@ -39,8 +42,11 @@ t.datetime "updated_at", null: false t.integer "vote_count", default: 0 t.integer "publication_year" + t.integer "user_id" + t.index ["user_id"], name: "index_works_on_user_id", using: :btree end add_foreign_key "votes", "users" add_foreign_key "votes", "works" + add_foreign_key "works", "users" end