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

Done #1087

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Done #1087

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
Empty file added .env
Empty file.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ gem 'turbolinks'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
gem 'omniauth'
gem 'omniauth-facebook'
gem 'dotenv-rails'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
Expand Down
30 changes: 30 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,27 @@ GEM
concurrent-ruby (1.1.5)
crass (1.0.5)
daemons (1.3.1)
dotenv (2.7.6)
dotenv-rails (2.7.6)
dotenv (= 2.7.6)
railties (>= 3.2)
erubis (2.7.0)
eventmachine (1.2.7)
execjs (2.7.0)
faraday (1.2.0)
multipart-post (>= 1.2, < 3)
ruby2_keywords
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.2)
loofah (2.3.1)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
Expand All @@ -73,9 +82,26 @@ GEM
mini_mime (1.0.2)
mini_portile2 (2.4.0)
minitest (5.11.3)
multi_json (1.15.0)
multi_xml (0.6.0)
multipart-post (2.1.1)
nio4r (2.4.0)
nokogiri (1.10.8)
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 (8.0.0)
omniauth-oauth2 (~> 1.2)
omniauth-oauth2 (1.7.0)
oauth2 (~> 1.4)
omniauth (~> 1.9)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
Expand Down Expand Up @@ -109,6 +135,7 @@ GEM
rb-fsevent (0.10.3)
rb-inotify (0.10.0)
ffi (~> 1.0)
ruby2_keywords (0.0.2)
sass (3.7.4)
sass-listen (~> 4.0.0)
sass-listen (4.0.0)
Expand Down Expand Up @@ -153,7 +180,10 @@ PLATFORMS
DEPENDENCIES
byebug
coffee-rails (~> 4.1.0)
dotenv-rails
jquery-rails
omniauth
omniauth-facebook
pry
rails (~> 5.0)
sass-rails (~> 5.0)
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/sessions.coffee
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/sessions.scss
Original file line number Diff line number Diff line change
@@ -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/
21 changes: 21 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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
2 changes: 2 additions & 0 deletions app/helpers/sessions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module SessionsHelper
end
3 changes: 3 additions & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class User < ApplicationRecord
end
10 changes: 10 additions & 0 deletions app/views/welcome/home.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
<%# Add the Facebook login link here %>
<%= link_to('Log in with Facebook!', '/auth/facebook') %>

<% if session[:user_id] %>
<h1><%= @user.name %></h1>
<h2>Email: <%= @user.email %></h2>
<h2>Facebook UID: <%= @user.uid %></h2>
<img src="<%= @user.image %>">
<% else %>
<%= link_to('Log in with Facebook!', '/auth/facebook') %>
<% end %>
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 :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET']
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end

get '/auth/facebook/callback' => 'sessions#create'
end
12 changes: 12 additions & 0 deletions db/migrate/20201230211028_create_users.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 10 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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: 20201230211028) 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
13 changes: 13 additions & 0 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class UserTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end