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

Welcome page and oauth #29

Merged
merged 2 commits into from
Aug 20, 2020
Merged
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
8 changes: 8 additions & 0 deletions .generators
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<Settings><!--This file was automatically generated by Ruby plugin.
You are allowed to:
1. Reorder generators
2. Remove generators
3. Add installed generators
To add new installed generators automatically delete this file and reload the project.
--><GeneratorsGroup><Generator name="active_record:migration" /><Generator name="active_record:model" /><Generator name="active_record:observer" /><Generator name="active_record:session_migration" /><Generator name="controller" /><Generator name="erb:controller" /><Generator name="erb:mailer" /><Generator name="erb:scaffold" /><Generator name="generator" /><Generator name="helper" /><Generator name="integration_test" /><Generator name="mailer" /><Generator name="metal" /><Generator name="migration" /><Generator name="model" /><Generator name="model_subclass" /><Generator name="observer" /><Generator name="performance_test" /><Generator name="plugin" /><Generator name="resource" /><Generator name="scaffold" /><Generator name="scaffold_controller" /><Generator name="session_migration" /><Generator name="stylesheets" /><Generator name="test_unit:controller" /><Generator name="test_unit:helper" /><Generator name="test_unit:integration" /><Generator name="test_unit:mailer" /><Generator name="test_unit:model" /><Generator name="test_unit:observer" /><Generator name="test_unit:performance" /><Generator name="test_unit:plugin" /><Generator name="test_unit:scaffold" /></GeneratorsGroup></Settings>
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@

# Ignore test coverage
/coverage/*

# Ignore application configuration
/config/application.yml
.idea
7 changes: 7 additions & 0 deletions .rakeTasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Settings><!--This file was automatically generated by Ruby plugin.
You are allowed to:
1. Remove rake task
2. Add existing rake tasks
To add existing rake tasks automatically delete this file and reload the project.
--><RakeGroup description="" fullCmd="" taksId="rake" /></Settings>
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ rbenv:
addons:
postgresql: 9.6
script:
- bundle exec rails db:{create,migrate} RAILS_ENV=test
- bundle exec rails db:{create,migrate,seed} RAILS_ENV=test
- bundle exec rspec
deploy:
provider: heroku
api_key:
app:
secure: xFascAxQSlb0Ky8KQNB3En6+z4Q8SSPqtlDfZc0pu3g4dVvKLuJzdqu57LcPnJ7eENTAC4iqmz+o7vDcFn/lR/SKug1xGS9+165nK+nGVg6wohRq4DWpZHXH31mUk20ApkUztTZPLbFJj8uR8LpWiU5p0xDxhqt8K5Y9pAiuzG0bU2FDHKGSnQpW0gfnnQ73/mCORcXkvR3qY4q6vqNXs1Z59TdUo0wMMBHiW7VT1wV5mm27mTrNKR8+UDKVBJA71paL55F11C14hHpDWCeHS7D6KEPSj9oQLzLoEapwuEpoOxsG2L8VsSrsdOGdpSYhUOBCShYVVSGZocsEroqQ8IEXZzOuZqv83LBzrulyBnSbncp+J7cBUSY507dYlmVQGwT4OnTLBdqryjcoWiUM8HbEOfJUw3DLKPqwuoI6cG5YO1JS4j04iw5g/qocBGSTRaXbVaCHlqQetBLTSr8vdtOTlaBGKURQ3GsBdjHr+oEKSJjMZJnM3yOaX2msMg8X0qwf4XmP8zo+gGW2j79tSEDp44QSxsWmrjc/14Lc0c6sV4xL+f56evTSpC5ayw040eLnQXqvvtQ5gzq4O26wlFUSK5tgE4fedZ8IraqceWDQv3X9JUvPAtgl+tHf9QgHWJTYLixRZsRqj2q3R1NQqYPwB0dT4nTlfyfKjUwJBao=
app: movie-screening
on:
repo: backburnerstudios/viewing_party
branch: main
skip_cleanup: 'true'
run: rails db:migrate
8 changes: 7 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,30 @@ gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'
gem 'bootsnap'
gem 'jbuilder', '~> 2.5'
gem 'omniauth-google-oauth2'

group :development, :test do
gem 'pry'
gem 'travis'
gem 'figaro'
end

group :development do
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'rubocop-rails'
gem 'travis'
gem 'brakeman'
end

group :test do
gem 'rspec-rails'
gem 'capybara'
gem 'launchy'
gem 'simplecov'
gem 'factory_bot_rails'
gem 'simplecov'
gem 'faker'
gem 'shoulda-matchers'
end

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
37 changes: 37 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ GEM
bindex (0.8.1)
bootsnap (1.4.7)
msgpack (~> 1.0)
brakeman (4.9.0)
builder (3.2.4)
capybara (3.33.0)
addressable
Expand All @@ -74,11 +75,20 @@ GEM
ethon (0.12.0)
ffi (>= 1.3.0)
execjs (2.7.0)
factory_bot (6.1.0)
activesupport (>= 5.0.0)
factory_bot_rails (6.1.0)
factory_bot (~> 6.1.0)
railties (>= 5.0.0)
faker (2.13.0)
i18n (>= 1.6, < 2)
faraday (1.0.1)
multipart-post (>= 1.2, < 3)
faraday_middleware (1.0.0)
faraday (~> 1.0)
ffi (1.13.1)
figaro (1.2.0)
thor (>= 0.14.0, < 2)
gh (0.18.0)
activesupport (~> 5.0)
addressable (~> 2.4)
Expand All @@ -89,12 +99,14 @@ GEM
net-http-pipeline
globalid (0.4.2)
activesupport (>= 4.2.0)
hashie (4.1.0)
highline (2.0.3)
i18n (1.8.5)
concurrent-ruby (~> 1.0)
jbuilder (2.10.0)
activesupport (>= 5.0.0)
json (2.3.1)
jwt (2.2.1)
launchy (2.4.3)
addressable (~> 2.3)
listen (3.1.5)
Expand All @@ -115,12 +127,29 @@ GEM
minitest (5.14.1)
msgpack (1.3.3)
multi_json (1.15.0)
multi_xml (0.6.0)
multipart-post (2.1.1)
net-http-persistent (2.9.4)
net-http-pipeline (1.0.1)
nio4r (2.5.2)
nokogiri (1.10.10)
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-google-oauth2 (0.8.0)
jwt (>= 2.0)
omniauth (>= 1.1.1)
omniauth-oauth2 (>= 1.6)
omniauth-oauth2 (1.6.0)
oauth2 (~> 1.1)
omniauth (~> 1.9)
parallel (1.19.2)
parser (2.7.1.4)
ast (~> 2.4.1)
Expand Down Expand Up @@ -212,6 +241,8 @@ GEM
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
shoulda-matchers (4.3.0)
activesupport (>= 4.2.0)
simplecov (0.18.5)
docile (~> 1.1)
simplecov-html (~> 0.11)
Expand Down Expand Up @@ -259,18 +290,24 @@ PLATFORMS

DEPENDENCIES
bootsnap
brakeman
capybara
coffee-rails (~> 4.2)
factory_bot_rails
faker
figaro
jbuilder (~> 2.5)
launchy
listen (>= 3.0.5, < 3.2)
omniauth-google-oauth2
pg (>= 0.18, < 2.0)
pry
puma (~> 3.7)
rails (= 5.2.4.3)
rspec-rails
rubocop-rails
sass-rails (~> 5.0)
shoulda-matchers
simplecov
travis
tzinfo-data
Expand Down
40 changes: 34 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
# Viewing Party

This is the base repo for the viewing party project used for Turing's Backend Module 3.
[![Build Status](https://travis-ci.org/backburnerstudios/viewing_party.svg?branch=main)](https://travis-ci.org/backburnerstudios/viewing_party)

This application is a means to explore movies and create viewing parties for you and friends.

### About this Project
The application utilizes [Google OAuth](https://developers.google.com/identity/protocols/oauth2)
for [Calendar API access](https://developers.google.com/calendar) to save these events. We also
use the [Movie DB API](https://developers.themoviedb.org/3/getting-started/introduction) for movie data.

Viewing party is an application in which users can explore movie options and create a viewing party event for the user and friend's.
Example wireframes to follow are found [here](https://backend.turing.io/module3/projects/viewing_party/wireframes)

## Learning Goals of the Project

- Consume JSON APIs that require authentication
- Build an application that authenticates using OAuth
- Implement a self-referential relationship in ActiveRecord
- Utilize Continuous Integration using Travis CI
- Organize and refactor code to be more maintainable
- Apply RuboCop’s style guide for code quality
- Deploy to Heroku

#### Extension / Exploration Goals (1 extension is required)

- Send email from a Rails application
- Use ActionCable for chat functionality
- Implement front-end JavaScript for more dynamic pages
- Extend movie exploration by consuming additional API endpoints
- Deploy with another hosting provider

## Project Board and Hosting

- Project Board: https://github.com/backburnerstudios/viewing_party/projects/1
- Live site: https://movie-screening.herokuapp.com/

## Local Setup

1. Fork and Clone the repo
2. Install gem packages: `bundle install`
3. Setup the database: `rails db:create`
3. Setup the database: `rails db:{dropcreate,migrate,seed}`
4. Run all tests: `rspec`


## Versions
## Tech Stack Versions

- Ruby 2.5.3

- Rails 5.2.4.3


5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :current_user

def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
15 changes: 15 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class SessionsController < ApplicationController
def create
access_data = request.env['omniauth.auth']
user = User.parse_omniauth(access_data)
user.google_token = access_data.credentials.token

refresh_token = access_data.credentials.refresh_token
user.google_refresh_token = refresh_token if refresh_token.present?

user.save

session[:user_id] = user.id
redirect_to root_path
end
end
5 changes: 5 additions & 0 deletions app/controllers/welcome_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class WelcomeController < ApplicationController
def index
# do something here?
end
end
12 changes: 12 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class User < ApplicationRecord
validates_presence_of :username
validates :uid, presence: true, uniqueness: true
validates :google_token, presence: true, uniqueness: true

def self.parse_omniauth(access_data)
where(uid: access_data.info.uid).first_or_initialize do |user|
user.username = access_data.info.email
user.uid = access_data.uid
end
end
end
14 changes: 14 additions & 0 deletions app/views/welcome/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<h1>Welcome to the Viewing Party!</h1>

<% if current_user %>
<p>Welcome back, <%= current_user.username %>!</p>
<% else %>
<%= link_to 'Log in with Google', '/auth/google_oauth2' %>
<% end %>

<hr>

<h2>This application is a means to explore movies and create viewing parties for you and friends.</h2>

Utilizing <%= link_to 'the Movie DB API', 'https://www.themoviedb.org/' %> and the power of
<%= link_to 'Google Calendar', 'https://calendar.google.com' %>, you can create a viewing party with your friends!
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 :google_oauth2, ENV['GOOGLE_CLIENT_ID'], ENV['GOOGLE_CLIENT_SECRET'], {scope: "userinfo.email, calendar"}
end
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root to: 'welcome#index'

get '/auth/:provider/callback', to: 'sessions#create'
get '/auth/failure', to: redirect('/')
end
14 changes: 14 additions & 0 deletions db/migrate/20200820043729_create_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateUsers < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
t.string :uid
t.string :username
t.string :google_token
t.string :google_refresh_token

t.timestamps
end
add_index :users, :uid
add_index :users, :username
end
end
29 changes: 29 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_08_20_043729) 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 "uid"
t.string "username"
t.string "google_token"
t.string "google_refresh_token"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["uid"], name: "index_users_on_uid"
t.index ["username"], name: "index_users_on_username"
end

end
10 changes: 10 additions & 0 deletions spec/factories/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'faker'

FactoryBot.define do
factory :user do
username { "#{Faker::Games::WorldOfWarcraft.hero} #{Faker::Number.number(digits: 3)}" }
uid { Faker::Number.within(range: 100000..999999) }
google_token { Faker::Number.within(range: 100000..999999) }
google_refresh_token { Faker::Number.within(range: 100000..999999) }
end
end
14 changes: 14 additions & 0 deletions spec/features/sessions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'rails_helper'

RSpec.describe 'Sessions spec', type: :feature do
it 'logs the user in via google mock' do
stub_omniauth
user = create(:user, username: '[email protected]')

visit root_path

click_link 'Log in with Google'

expect(page).to have_content("Welcome back, #{user.username}")
end
end
Loading