From c45cb26857739d76fd32368dcfa2c26a2e18b24b Mon Sep 17 00:00:00 2001 From: Rana Sulaiman Date: Mon, 17 Apr 2017 15:37:02 -0700 Subject: [PATCH 01/11] Built GitHub Credential --- .gitignore | 1 + Gemfile | 6 ++++++ Gemfile.lock | 36 ++++++++++++++++++++++++++++++++- config/initializers/omniauth.rb | 3 +++ 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 config/initializers/omniauth.rb diff --git a/.gitignore b/.gitignore index 48fb168..d869f9f 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ # Ignore Byebug command history file. .byebug_history +.env diff --git a/Gemfile b/Gemfile index 24756e7..1a3ae02 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,9 @@ source 'https://rubygems.org' +gem "awesome_print" +gem "omniauth" +gem "omniauth-github" + ruby '2.4.0' git_source(:github) do |repo_name| @@ -45,6 +49,7 @@ group :development, :test do # Improve the error message you get in the browser gem 'better_errors' + gem "binding_of_caller" # Use pry for rails console gem 'pry-rails' @@ -64,6 +69,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 diff --git a/Gemfile.lock b/Gemfile.lock index 4d99ffe..3b0e348 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -40,6 +40,7 @@ GEM tzinfo (~> 1.1) ansi (1.5.0) arel (7.1.4) + awesome_print (1.7.0) babel-source (5.8.35) babel-transpiler (0.7.0) babel-source (>= 4.0, < 6) @@ -49,6 +50,8 @@ GEM erubis (>= 2.6.6) rack (>= 0.9.0) bindex (0.5.0) + binding_of_caller (0.7.2) + debug_inspector (>= 0.0.1) builder (3.2.3) byebug (9.0.6) coderay (1.1.1) @@ -60,8 +63,15 @@ GEM execjs coffee-script-source (1.12.2) concurrent-ruby (1.0.5) + debug_inspector (0.0.2) + dotenv (2.2.0) + dotenv-rails (2.2.0) + dotenv (= 2.2.0) + railties (>= 3.2, < 5.1) erubis (2.7.0) execjs (2.7.0) + faraday (0.11.0) + multipart-post (>= 1.2, < 3) ffi (1.9.18) foundation-rails (6.3.0.0) railties (>= 3.1.0) @@ -69,6 +79,7 @@ GEM sprockets-es6 (>= 0.9.0) globalid (0.3.7) activesupport (>= 4.1.0) + hashie (3.5.5) i18n (0.8.1) jbuilder (2.6.3) activesupport (>= 3.0.0, < 5.2) @@ -77,6 +88,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 +116,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.3.1) + faraday (>= 0.8, < 0.12) + jwt (~> 1.0) + multi_json (~> 1.3) + multi_xml (~> 0.5) + rack (>= 1.2, < 3) + omniauth (1.6.1) + hashie (>= 3.4.6, < 3.6.0) + rack (>= 1.6.2, < 3) + omniauth-github (1.2.3) + 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) @@ -193,9 +222,12 @@ PLATFORMS ruby DEPENDENCIES + awesome_print better_errors + binding_of_caller byebug coffee-rails (~> 4.2) + dotenv-rails foundation-rails jbuilder (~> 2.5) jquery-rails @@ -204,6 +236,8 @@ DEPENDENCIES minitest-reporters minitest-skip minitest-spec-rails + omniauth + omniauth-github pg (~> 0.18) pry-rails puma (~> 3.0) @@ -220,4 +254,4 @@ RUBY VERSION ruby 2.4.0p0 BUNDLED WITH - 1.14.4 + 1.14.6 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 From 39dd51e1fb85de9d00080104aa3c23bf7f4397ca Mon Sep 17 00:00:00 2001 From: Rana Sulaiman Date: Tue, 18 Apr 2017 09:03:27 -0700 Subject: [PATCH 02/11] Wave1 is done --- app/controllers/sessions_controller.rb | 61 +++++++++++++------ app/models/user.rb | 17 +++++- app/views/layouts/application.html.erb | 4 +- app/views/sessions/login_form.html.erb | 4 +- app/views/users/index.html.erb | 2 +- app/views/works/show.html.erb | 2 +- config/routes.rb | 6 +- .../20170417230906_delete_user_nmae_column.rb | 5 ++ db/migrate/20170417231237_add_columns.rb | 9 +++ db/schema.rb | 7 ++- test/controllers/sessions_controller_test.rb | 16 ++--- 11 files changed, 94 insertions(+), 39 deletions(-) create mode 100644 db/migrate/20170417230906_delete_user_nmae_column.rb create mode 100644 db/migrate/20170417231237_add_columns.rb diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 5bce99e..f9dd2c1 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,30 +1,51 @@ class SessionsController < ApplicationController - def login_form - end + # def login_form + # end + def create + auth_hash = request.env['omniauth.auth'] - 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}" + if auth_hash["uid"] + user = User.find_by(uid: auth_hash["uid"], provider: auth_hash["provider"]) + + if user.nil? + user = User.create_from_github(auth_hash) 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[:success] = "Logged in successfully" + redirect_to root_path end + session[:user_id] = user.id + else + flash[:error] = "Could not log on" + redirect_to root_path end - redirect_to root_path + 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 diff --git a/app/models/user.rb b/app/models/user.rb index 4cac8fe..8ec07b6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,5 +2,20 @@ class User < ApplicationRecord has_many :votes has_many :ranked_works, through: :votes, source: :work - validates :username, uniqueness: true, presence: true + validates :name, uniqueness: true, presence: true + + def self.create_from_github(auth_hash) + + user = User.new + user.uid = auth_hash["uid"] + user.provider = auth_hash["provider"] + user.name = auth_hash["info"]["nickname"] + user.email = auth_hash["info"]["email"] + #binding.pry + + user.save ? user : nil + + end + + end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 9231526..7b87210 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -19,11 +19,11 @@
<% if @login_user %> -

Logged in as <%= link_to @login_user.username, user_path(@login_user) %>

+

Logged in as <%= link_to @login_user.name, user_path(@login_user) %>

<%= link_to "Log Out", logout_path, method: :post, class: "button float-right" %> <% else %>

Not logged in

- <%= link_to "Log In", login_path, class: "button float-right" %> + <%= link_to "Log In", '/auth/github', class: "button float-right" %> <% end %>
diff --git a/app/views/sessions/login_form.html.erb b/app/views/sessions/login_form.html.erb index dccb9bd..ad62d82 100644 --- a/app/views/sessions/login_form.html.erb +++ b/app/views/sessions/login_form.html.erb @@ -1,7 +1,7 @@

Log In

<%= form_tag do %> - <%= label_tag(:username, "Username") %> - <%= text_field_tag(:username) %> + <%= label_tag(:name, "name") %> + <%= text_field_tag(:name) %> <%= submit_tag("Log In", class: "button") %> <% end %> diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index 83570de..72dfc69 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -10,7 +10,7 @@ <% @users.each do |user| %> - <%= link_to user.username, user_path(user) %> + <%= link_to user.name, user_path(user) %> <%= user.votes.count %> <%= render_date user.created_at %> diff --git a/app/views/works/show.html.erb b/app/views/works/show.html.erb index 85faecc..1d766f1 100644 --- a/app/views/works/show.html.erb +++ b/app/views/works/show.html.erb @@ -23,7 +23,7 @@ <% @votes.each do |vote| %> - <%= link_to vote.user.username, user_path(vote.user) %> + <%= link_to vote.user.name, user_path(vote.user) %> <%= render_date vote.created_at %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 4352c93..138bd4d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,8 +1,10 @@ Rails.application.routes.draw do # 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' + + get "/auth/:provider/callback", to: "sessions#create" + # get '/login', to: 'sessions#login_form', as: 'login' + # post '/login', to: 'sessions#login' post '/logout', to: 'sessions#logout', as: 'logout' # Build the category routes for albums, books and movies diff --git a/db/migrate/20170417230906_delete_user_nmae_column.rb b/db/migrate/20170417230906_delete_user_nmae_column.rb new file mode 100644 index 0000000..d27c26c --- /dev/null +++ b/db/migrate/20170417230906_delete_user_nmae_column.rb @@ -0,0 +1,5 @@ +class DeleteUserNmaeColumn < ActiveRecord::Migration[5.0] + def change + remove_column :users, :username + end +end diff --git a/db/migrate/20170417231237_add_columns.rb b/db/migrate/20170417231237_add_columns.rb new file mode 100644 index 0000000..6f6906d --- /dev/null +++ b/db/migrate/20170417231237_add_columns.rb @@ -0,0 +1,9 @@ +class AddColumns < ActiveRecord::Migration[5.0] + def change + add_column :users, :name, :string + add_column :users, :email, :string, null: false + add_column :users, :uid, :integer, null: false + add_column :users, :provider, :string, null: false + + end +end diff --git a/db/schema.rb b/db/schema.rb index 6bc8ba5..ae46509 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,15 +10,18 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170407164321) do +ActiveRecord::Schema.define(version: 20170417231237) 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" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "name" + t.string "email", null: false + t.integer "uid", null: false + t.string "provider", null: false end create_table "votes", force: :cascade do |t| diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb index 9efd128..8d23967 100644 --- a/test/controllers/sessions_controller_test.rb +++ b/test/controllers/sessions_controller_test.rb @@ -21,27 +21,27 @@ # where there's an interesting difference between a logged-in # and not-logged-in user. it "succeeds for a new user" do - username = "test_user" + name = "test_user" # Precondition: no user with this username exists - User.find_by(username: username).must_be_nil + User.find_by(name: name).must_be_nil - post login_path, params: { username: username } + post login_path, params: { name: name } must_redirect_to root_path end it "succeeds for a returning user" do - username = User.first.username - post login_path, params: { username: username } + name = User.first.name + post login_path, params: { name: name } must_redirect_to root_path end - it "renders 400 bad_request if the username is blank" do - post login_path, params: { username: "" } + it "renders 400 bad_request if the name is blank" do + post login_path, params: { name: "" } must_respond_with :bad_request end it "succeeds if a different user is already logged in" do - username = "user_1" + name = "user_1" post login_path, params: { username: username } must_redirect_to root_path From 943bd65154945778969cf16c22b92b76ea916ba5 Mon Sep 17 00:00:00 2001 From: Rana Sulaiman Date: Tue, 18 Apr 2017 12:31:38 -0700 Subject: [PATCH 03/11] Wave2 is done --- app/controllers/application_controller.rb | 22 ++++++++++++++++++++++ app/controllers/sessions_controller.rb | 7 ++----- app/controllers/works_controller.rb | 3 +++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 087352a..2e6b426 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,11 +3,33 @@ class ApplicationController < ActionController::Base before_action :find_user + before_action :require_login +helper_method :current_user + +def require_login + #See if we have a logged-in user (probably using session) + if !session[:user_id] + #if not logged-in + #Show message about not being logged-in and send to root route + flash[:status] = :failure + flash[:result_text] = "You must be logged in to view this page" + redirect_to root_path + end + #Let it be if there is someone logged-in +end + + def render_404 # DPR: supposedly this will actually render a 404 page in production raise ActionController::RoutingError.new('Not Found') end + def current_user + # MEMOIZE: storing something so yo don't have to retrieve it all the time + @logged_in_user ||= User.find(session[:user_id]) if session[:user_id] + end + + private def find_user if session[:user_id] diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index f9dd2c1..3d7b3bc 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,6 +1,6 @@ class SessionsController < ApplicationController - # def login_form - # end + skip_before_action :require_login, only: [:create] + def create auth_hash = request.env['omniauth.auth'] @@ -20,9 +20,6 @@ def create end end - - - # def login # username = params[:username] # if username and user = User.find_by(username: username) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 8ac0bcc..4b31a64 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -1,8 +1,11 @@ class WorksController < ApplicationController # We should always be able to tell what category # of work we're dealing with + before_action :category_from_url, only: [:index, :new, :create] before_action :category_from_work, except: [:root, :index, :new, :create] + skip_before_action :require_login, only: [:root] + def root @albums = Work.best_albums From ce4df52b8a86923bd37e91c59018ddf91f8aecca Mon Sep 17 00:00:00 2001 From: Rana Sulaiman Date: Sat, 29 Apr 2017 23:04:58 -0700 Subject: [PATCH 04/11] Added user_id column to works model --- app/controllers/sessions_controller.rb | 7 +++---- db/migrate/20170430060218_add_user_id_column.rb | 5 +++++ db/schema.rb | 3 ++- 3 files changed, 10 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20170430060218_add_user_id_column.rb diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 3d7b3bc..30de398 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -9,13 +9,12 @@ def create if user.nil? user = User.create_from_github(auth_hash) - else - flash[:success] = "Logged in successfully" - redirect_to root_path end session[:user_id] = user.id + flash[:success] = "Logged in successfully" + redirect_to root_path else - flash[:error] = "Could not log on" + flash[:error] = "Could not log in" redirect_to root_path end diff --git a/db/migrate/20170430060218_add_user_id_column.rb b/db/migrate/20170430060218_add_user_id_column.rb new file mode 100644 index 0000000..72880f3 --- /dev/null +++ b/db/migrate/20170430060218_add_user_id_column.rb @@ -0,0 +1,5 @@ +class AddUserIdColumn < ActiveRecord::Migration[5.0] + def change + add_column :works, :user_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index ae46509..b43e7d5 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: 20170417231237) do +ActiveRecord::Schema.define(version: 20170430060218) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -42,6 +42,7 @@ t.datetime "updated_at", null: false t.integer "vote_count", default: 0 t.integer "publication_year" + t.integer "user_id" end add_foreign_key "votes", "users" From e141b5adcbc8d90e989c2f5b194b058d022c2410 Mon Sep 17 00:00:00 2001 From: Rana Sulaiman Date: Sat, 29 Apr 2017 23:09:21 -0700 Subject: [PATCH 05/11] Added work/user model relations --- app/models/user.rb | 2 +- app/models/work.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index 8ec07b6..0d05978 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,5 @@ class User < ApplicationRecord - has_many :votes + has_many :votes, :works has_many :ranked_works, through: :votes, source: :work validates :name, uniqueness: true, presence: true diff --git a/app/models/work.rb b/app/models/work.rb index 7b27fd3..098d18b 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -1,4 +1,5 @@ class Work < ApplicationRecord + belongs_to :user has_many :votes, dependent: :destroy has_many :ranking_users, through: :votes, source: :user From da12c6e9d02e12004f2c39143d6dc37abf2996b8 Mon Sep 17 00:00:00 2001 From: Rana Sulaiman Date: Sat, 29 Apr 2017 23:17:55 -0700 Subject: [PATCH 06/11] Added user_id hidden field to partial form --- app/controllers/works_controller.rb | 2 +- app/views/works/_form.html.erb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 4b31a64..87af0e6 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -94,7 +94,7 @@ def upvote private def media_params - params.require(:work).permit(:title, :category, :creator, :description, :publication_year) + params.require(:work).permit(:user_id, :title, :category, :creator, :description, :publication_year) end def category_from_url diff --git a/app/views/works/_form.html.erb b/app/views/works/_form.html.erb index 89e19a0..c2d8487 100644 --- a/app/views/works/_form.html.erb +++ b/app/views/works/_form.html.erb @@ -1,5 +1,6 @@
<%= form_for @work do |f| %> + <%= f.hidden_field :user_id, value: session[:user_id] %> <%= f.hidden_field :category, value: @work.category %> <%= f.label :title %> From 7388f842b124852b81dde86745bb099dc16e0aa7 Mon Sep 17 00:00:00 2001 From: Rana Sulaiman Date: Sat, 29 Apr 2017 23:36:56 -0700 Subject: [PATCH 07/11] Changed works#edit to only allow owner --- app/controllers/works_controller.rb | 5 +++++ app/models/user.rb | 3 ++- app/views/works/_form.html.erb | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 87af0e6..a339431 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -42,6 +42,11 @@ def show end def edit + if session[:user_id] != @work.user_id + flash[:status] = :failure + flash[:result_text] = "This #{@media_category.singularize} can only be updated by its owner." + redirect_to works_path(@media_category) + end end def update diff --git a/app/models/user.rb b/app/models/user.rb index 0d05978..08f2a05 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,5 +1,6 @@ class User < ApplicationRecord - has_many :votes, :works + has_many :votes + has_many :works has_many :ranked_works, through: :votes, source: :work validates :name, uniqueness: true, presence: true diff --git a/app/views/works/_form.html.erb b/app/views/works/_form.html.erb index c2d8487..f2e170c 100644 --- a/app/views/works/_form.html.erb +++ b/app/views/works/_form.html.erb @@ -1,6 +1,6 @@
<%= form_for @work do |f| %> - <%= f.hidden_field :user_id, value: session[:user_id] %> + <%= f.hidden_field :user_id, value: @work.user_id %> <%= f.hidden_field :category, value: @work.category %> <%= f.label :title %> From 0dd419a3f3faf508630b71b70821f41461b4e68e Mon Sep 17 00:00:00 2001 From: Rana Sulaiman Date: Sat, 29 Apr 2017 23:43:26 -0700 Subject: [PATCH 08/11] Changed works#destroy to only allow owner --- app/controllers/works_controller.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index a339431..c3cfee8 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -64,10 +64,16 @@ def update end def destroy - @work.destroy - flash[:status] = :success - flash[:result_text] = "Successfully destroyed #{@media_category.singularize} #{@work.id}" - redirect_to root_path + if session[:user_id] != @work.user_id + flash[:status] = :failure + flash[:result_text] = "This #{@media_category.singularize} can only be deleted by its owner." + redirect_to works_path(@media_category) + else + @work.destroy + flash[:status] = :success + flash[:result_text] = "Successfully destroyed #{@media_category.singularize} #{@work.id}" + redirect_to root_path + end end def upvote From 505f19fe9c1edf0612743f261850f0e513c04f84 Mon Sep 17 00:00:00 2001 From: Rana Sulaiman Date: Sat, 29 Apr 2017 23:50:57 -0700 Subject: [PATCH 09/11] Added user_id to works#new Work.new argument --- app/controllers/works_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index c3cfee8..b84a2cd 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -20,7 +20,7 @@ def index end def new - @work = Work.new(category: @media_category) + @work = Work.new(user_id: session[:user_id], category: @media_category) end def create From b0a7b788a319ea3f23716eceaeaaab1a32d0965e Mon Sep 17 00:00:00 2001 From: Rana Sulaiman Date: Sun, 30 Apr 2017 00:01:09 -0700 Subject: [PATCH 10/11] Added omniauth-google-oauth2 gem --- Gemfile | 1 + Gemfile.lock | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/Gemfile b/Gemfile index 1a3ae02..bef22bc 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,7 @@ source 'https://rubygems.org' gem "awesome_print" gem "omniauth" gem "omniauth-github" +gem "omniauth-google-oauth2" ruby '2.4.0' diff --git a/Gemfile.lock b/Gemfile.lock index 3b0e348..75e24cc 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -133,6 +133,11 @@ GEM omniauth-github (1.2.3) omniauth (~> 1.5) omniauth-oauth2 (>= 1.4.0, < 2.0) + omniauth-google-oauth2 (0.4.1) + jwt (~> 1.5.2) + multi_json (~> 1.3) + omniauth (>= 1.1.1) + omniauth-oauth2 (>= 1.3.1) omniauth-oauth2 (1.4.0) oauth2 (~> 1.0) omniauth (~> 1.2) @@ -238,6 +243,7 @@ DEPENDENCIES minitest-spec-rails omniauth omniauth-github + omniauth-google-oauth2 pg (~> 0.18) pry-rails puma (~> 3.0) From 40539ad0c3ceb568324feb244c06805d12692953 Mon Sep 17 00:00:00 2001 From: Rana Sulaiman Date: Sun, 30 Apr 2017 10:26:13 -0700 Subject: [PATCH 11/11] Login using Google is done --- app/controllers/application_controller.rb | 21 +++++++++---------- app/controllers/sessions_controller.rb | 7 ++++++- app/models/user.rb | 14 +++++++++---- app/views/layouts/application.html.erb | 3 ++- config/initializers/omniauth.rb | 1 + config/routes.rb | 2 ++ .../20170430171350_change_ui_dto_string.rb | 5 +++++ db/schema.rb | 4 ++-- 8 files changed, 38 insertions(+), 19 deletions(-) create mode 100644 db/migrate/20170430171350_change_ui_dto_string.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2e6b426..d791af2 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -6,18 +6,17 @@ class ApplicationController < ActionController::Base before_action :require_login helper_method :current_user -def require_login - #See if we have a logged-in user (probably using session) - if !session[:user_id] - #if not logged-in - #Show message about not being logged-in and send to root route - flash[:status] = :failure - flash[:result_text] = "You must be logged in to view this page" - redirect_to root_path + def require_login + #See if we have a logged-in user (probably using session) + if !session[:user_id] + #if not logged-in + #Show message about not being logged-in and send to root route + flash[:status] = :failure + flash[:result_text] = "You must be logged in to view this page" + redirect_to root_path + end + #Let it be if there is someone logged-in end - #Let it be if there is someone logged-in -end - def render_404 # DPR: supposedly this will actually render a 404 page in production diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 30de398..df41e5f 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -3,12 +3,17 @@ class SessionsController < ApplicationController def create auth_hash = request.env['omniauth.auth'] + auth_hash["uid"] = auth_hash["uid"].to_s if auth_hash["uid"] user = User.find_by(uid: auth_hash["uid"], provider: auth_hash["provider"]) if user.nil? - user = User.create_from_github(auth_hash) + user = User.create_from_omniauth(auth_hash) + if user.nil? + flash[:error] = "Unable to log you in." + redirect_to root_path + end end session[:user_id] = user.id flash[:success] = "Logged in successfully" diff --git a/app/models/user.rb b/app/models/user.rb index 08f2a05..e8375be 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,16 +5,22 @@ class User < ApplicationRecord validates :name, uniqueness: true, presence: true - def self.create_from_github(auth_hash) + def self.create_from_omniauth(auth_hash) user = User.new user.uid = auth_hash["uid"] user.provider = auth_hash["provider"] - user.name = auth_hash["info"]["nickname"] + + if auth_hash["info"]["nickname"] + user.name = auth_hash["info"]["nickname"] + else + user.name = auth_hash["info"]["name"] + end + user.email = auth_hash["info"]["email"] #binding.pry - - user.save ? user : nil + user.save + return user end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 7b87210..a372d61 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -23,7 +23,8 @@ <%= link_to "Log Out", logout_path, method: :post, class: "button float-right" %> <% else %>

Not logged in

- <%= link_to "Log In", '/auth/github', class: "button float-right" %> + <%= link_to "Log In using Github", '/auth/github', class: "button float-right" %> + <%= link_to "Log In using Google", '/auth/google_oauth2', class: "button float-right" %> <% end %> diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb index fd44161..562489e 100644 --- a/config/initializers/omniauth.rb +++ b/config/initializers/omniauth.rb @@ -1,3 +1,4 @@ Rails.application.config.middleware.use OmniAuth::Builder do provider :github, ENV["GITHUB_CLIENT_ID"], ENV["GITHUB_CLIENT_SECRET"], scope: "user:email" + provider :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"]#, scope: "user:email" end diff --git a/config/routes.rb b/config/routes.rb index 138bd4d..ae2aa34 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,8 @@ root 'works#root' get "/auth/:provider/callback", to: "sessions#create" + #get "/auth/google_oauth2/callback", to: "sessions#create" + # get '/login', to: 'sessions#login_form', as: 'login' # post '/login', to: 'sessions#login' post '/logout', to: 'sessions#logout', as: 'logout' diff --git a/db/migrate/20170430171350_change_ui_dto_string.rb b/db/migrate/20170430171350_change_ui_dto_string.rb new file mode 100644 index 0000000..e2f21a2 --- /dev/null +++ b/db/migrate/20170430171350_change_ui_dto_string.rb @@ -0,0 +1,5 @@ +class ChangeUiDtoString < ActiveRecord::Migration[5.0] + def change + change_column :users, :uid, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index b43e7d5..5c4e207 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: 20170430060218) do +ActiveRecord::Schema.define(version: 20170430171350) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -20,7 +20,7 @@ t.datetime "updated_at", null: false t.string "name" t.string "email", null: false - t.integer "uid", null: false + t.string "uid", null: false t.string "provider", null: false end