From 912540316479ed978ba677d772554a4e94122d26 Mon Sep 17 00:00:00 2001 From: Sara Harvey Date: Sun, 26 Apr 2020 18:55:42 +0000 Subject: [PATCH] Done. --- .env | 2 ++ .gitignore | 3 ++- Gemfile | 4 +++ Gemfile.lock | 28 ++++++++++++++++++++ app/assets/javascripts/sessions.coffee | 3 +++ app/assets/stylesheets/sessions.scss | 3 +++ app/controllers/sessions_controller.rb | 19 +++++++++++++ app/helpers/sessions_helper.rb | 2 ++ app/models/application_record.rb | 3 +++ app/models/user.rb | 2 ++ app/views/welcome/home.html.erb | 9 ++++++- config/initializers/omniauth.rb | 3 +++ config/routes.rb | 1 + db/migrate/20200426182746_create_users.rb | 12 +++++++++ db/schema.rb | 12 +++++++-- test/controllers/sessions_controller_test.rb | 7 +++++ test/fixtures/users.yml | 13 +++++++++ test/models/user_test.rb | 7 +++++ 18 files changed, 129 insertions(+), 4 deletions(-) create mode 100644 .env create mode 100644 app/assets/javascripts/sessions.coffee create mode 100644 app/assets/stylesheets/sessions.scss create mode 100644 app/controllers/sessions_controller.rb create mode 100644 app/helpers/sessions_helper.rb create mode 100644 app/models/application_record.rb create mode 100644 app/models/user.rb create mode 100644 config/initializers/omniauth.rb create mode 100644 db/migrate/20200426182746_create_users.rb create mode 100644 test/controllers/sessions_controller_test.rb create mode 100644 test/fixtures/users.yml create mode 100644 test/models/user_test.rb diff --git a/.env b/.env new file mode 100644 index 000000000..1f59b2cd1 --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +FACEBOOK_APP_ID=<532700687404457> +FACEBOOK_APP_SECRET=<68315a3ffad7b6c15aa36ea10332541e> \ No newline at end of file diff --git a/.gitignore b/.gitignore index d28c5de8e..1ca8fa957 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.sqlite3 *.log -tmp/Gemfile.lock \ No newline at end of file +tmp/Gemfile.lock +omniauth_readme-v-000/.env \ No newline at end of file diff --git a/Gemfile b/Gemfile index 72da4c4f4..ccef4bb00 100644 --- a/Gemfile +++ b/Gemfile @@ -43,4 +43,8 @@ group :development, :test do gem 'pry' end +gem 'omniauth' +gem 'omniauth-facebook' +gem 'dotenv-rails' + # Access an IRB console on exception pages or by using <%= console %> in views diff --git a/Gemfile.lock b/Gemfile.lock index f1add80b9..d6dcb6585 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -52,18 +52,26 @@ GEM concurrent-ruby (1.1.5) crass (1.0.5) daemons (1.3.1) + dotenv (2.7.5) + dotenv-rails (2.7.5) + dotenv (= 2.7.5) + railties (>= 3.2, < 6.1) erubis (2.7.0) eventmachine (1.2.7) execjs (2.7.0) + faraday (1.0.1) + multipart-post (>= 1.2, < 3) ffi (1.11.1) globalid (0.4.2) activesupport (>= 4.2.0) + hashie (4.1.0) i18n (1.6.0) concurrent-ruby (~> 1.0) jquery-rails (4.3.5) rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) + jwt (2.2.1) loofah (2.3.1) crass (~> 1.0.2) nokogiri (>= 1.5.9) @@ -73,9 +81,26 @@ GEM mini_mime (1.0.2) mini_portile2 (2.4.0) minitest (5.11.3) + multi_json (1.14.1) + multi_xml (0.6.0) + multipart-post (2.1.1) nio4r (2.4.0) nokogiri (1.10.5) mini_portile2 (~> 2.4.0) + oauth2 (1.4.4) + faraday (>= 0.8, < 2.0) + jwt (>= 1.0, < 3.0) + multi_json (~> 1.3) + multi_xml (~> 0.5) + rack (>= 1.2, < 3) + omniauth (1.9.1) + hashie (>= 3.4.6) + rack (>= 1.6.2, < 3) + omniauth-facebook (6.0.0) + omniauth-oauth2 (~> 1.2) + omniauth-oauth2 (1.6.0) + oauth2 (~> 1.1) + omniauth (~> 1.9) pry (0.12.2) coderay (~> 1.1.0) method_source (~> 0.9.0) @@ -153,7 +178,10 @@ PLATFORMS DEPENDENCIES byebug coffee-rails (~> 4.1.0) + dotenv-rails jquery-rails + omniauth + omniauth-facebook pry rails (~> 5.0) sass-rails (~> 5.0) diff --git a/app/assets/javascripts/sessions.coffee b/app/assets/javascripts/sessions.coffee new file mode 100644 index 000000000..24f83d18b --- /dev/null +++ b/app/assets/javascripts/sessions.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/sessions.scss b/app/assets/stylesheets/sessions.scss new file mode 100644 index 000000000..7bef9cf82 --- /dev/null +++ b/app/assets/stylesheets/sessions.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the sessions controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 000000000..58a0b72b9 --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,19 @@ +class SessionsController < ApplicationController + def create + @user = User.find_or_create_by(uid: auth['uid']) do |u| + u.name = auth['info']['name'] + u.email = auth['info']['email'] + u.image = auth['info']['image'] + end + + session[:user_id] = @user.id + + render 'welcome/home' + end + + private + + def auth + request.env['omniauth.auth'] + end +end \ No newline at end of file diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb new file mode 100644 index 000000000..309f8b2eb --- /dev/null +++ b/app/helpers/sessions_helper.rb @@ -0,0 +1,2 @@ +module SessionsHelper +end diff --git a/app/models/application_record.rb b/app/models/application_record.rb new file mode 100644 index 000000000..10a4cba84 --- /dev/null +++ b/app/models/application_record.rb @@ -0,0 +1,3 @@ +class ApplicationRecord < ActiveRecord::Base + self.abstract_class = true +end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 000000000..379658a50 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,2 @@ +class User < ApplicationRecord +end diff --git a/app/views/welcome/home.html.erb b/app/views/welcome/home.html.erb index 337e7f4b0..ccf44d1f5 100644 --- a/app/views/welcome/home.html.erb +++ b/app/views/welcome/home.html.erb @@ -1 +1,8 @@ -<%# Add the Facebook login link here %> +<% if session[:user_id] %> +

<%= @user.name %>

+

Email: <%= @user.email %>

+

Facebook UID: <%= @user.uid %>

+ +<% else %> + <%= link_to('Log in with Facebook!', '/auth/facebook') %> +<% end %> \ No newline at end of file diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb new file mode 100644 index 000000000..c3c380367 --- /dev/null +++ b/config/initializers/omniauth.rb @@ -0,0 +1,3 @@ +Rails.application.config.middleware.use OmniAuth::Builder do + provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'] +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index f7e854806..c34d5a2d8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,6 @@ Rails.application.routes.draw do root 'welcome#home' + get '/auth/facebook/callback' => 'sessions#create' # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/db/migrate/20200426182746_create_users.rb b/db/migrate/20200426182746_create_users.rb new file mode 100644 index 000000000..9800bfbcd --- /dev/null +++ b/db/migrate/20200426182746_create_users.rb @@ -0,0 +1,12 @@ +class CreateUsers < ActiveRecord::Migration[5.0] + def change + create_table :users do |t| + t.string :name + t.string :email + t.string :image + t.string :uid + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 4dfbb1680..f695de0d7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. @@ -11,6 +10,15 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 0) do +ActiveRecord::Schema.define(version: 20200426182746) do + + create_table "users", force: :cascade do |t| + t.string "name" + t.string "email" + t.string "image" + t.string "uid" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end end diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb new file mode 100644 index 000000000..6135ce6af --- /dev/null +++ b/test/controllers/sessions_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class SessionsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 000000000..9a75bd251 --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,13 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyString + email: MyString + image: MyString + uid: MyString + +two: + name: MyString + email: MyString + image: MyString + uid: MyString diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 000000000..82f61e010 --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class UserTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end