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

added countdown timer and FB authentication #1

Merged
merged 1 commit into from Jan 25, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion Gemfile
Expand Up @@ -56,4 +56,6 @@ gem 'spring', group: :development
gem 'bootstrap-sass' #Twitter Bootstrap, CSS styling
gem 'devise' #Devise, authentication
gem 'figaro' #Figaro, handles environmental variables
gem 'faker' #Faker, fake data
gem 'faker' #Faker, fake data
gem 'omniauth' #Omniauth, various authentication
gem 'omniauth-facebook' #OmniAuth Facebook, FB authentication
24 changes: 24 additions & 0 deletions Gemfile.lock
Expand Up @@ -64,8 +64,11 @@ GEM
railties (>= 3.0.0)
faker (1.4.3)
i18n (~> 0.5)
faraday (0.9.1)
multipart-post (>= 1.2, < 3)
figaro (1.0.0)
thor (~> 0.14)
hashie (3.3.2)
hike (1.2.3)
i18n (0.7.0)
jbuilder (2.2.6)
Expand All @@ -75,15 +78,34 @@ GEM
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
json (1.8.2)
jwt (1.2.0)
mail (2.6.3)
mime-types (>= 1.16, < 3)
method_source (0.8.2)
mime-types (2.4.3)
mini_portile (0.6.2)
minitest (5.5.1)
multi_json (1.10.1)
multi_xml (0.5.5)
multipart-post (2.0.0)
nokogiri (1.6.5)
mini_portile (~> 0.6.0)
oauth2 (1.0.0)
faraday (>= 0.8, < 0.10)
jwt (~> 1.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (~> 1.2)
omniauth (1.2.2)
hashie (>= 1.2, < 4)
rack (~> 1.0)
omniauth-facebook (2.0.0)
omniauth-oauth2 (~> 1.2)
omniauth-oauth2 (1.2.0)
faraday (>= 0.8, < 0.10)
multi_json (~> 1.3)
oauth2 (~> 1.0)
omniauth (~> 1.2)
orm_adapter (0.5.0)
pg (0.18.1)
pry (0.10.1)
Expand Down Expand Up @@ -186,6 +208,8 @@ DEPENDENCIES
figaro
jbuilder (~> 2.0)
jquery-rails
omniauth
omniauth-facebook
pg
pry-rails
rails (= 4.1.7)
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/application_controller.rb
Expand Up @@ -15,6 +15,6 @@ def after_sign_out_path_for(resource)
protected

def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :name
end
devise_parameter_sanitizer.for(:sign_up) << [:name, :provider, :uid]
end
end
15 changes: 15 additions & 0 deletions app/controllers/users/omniauth_callbacks_controller.rb
@@ -0,0 +1,15 @@
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController

def facebook
# You need to implement the method below in your model (e.g. app/models/user.rb)
@user = User.from_omniauth(request.env["omniauth.auth"])

if @user.persisted?
sign_in_and_redirect @user
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
7 changes: 6 additions & 1 deletion app/models/item.rb
@@ -1,3 +1,8 @@
class Item < ActiveRecord::Base
belongs_to :list
end

def days_left
7 - (DateTime.now.to_date - created_at.to_date).to_i
end

end
20 changes: 19 additions & 1 deletion app/models/user.rb
Expand Up @@ -2,7 +2,25 @@ class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
:recoverable, :rememberable, :trackable, :validatable, :confirmable, :omniauthable, :omniauth_providers => [:facebook]

has_one :list

def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
user.name = auth.info.name # assuming the user model has a name
user.skip_confirmation!
user.save!
end
end

def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
end
4 changes: 4 additions & 0 deletions app/views/devise/registrations/new.html.erb
Expand Up @@ -32,6 +32,10 @@
<%= f.submit "Sign up", class: 'btn btn-success' %>
</div>

<div class="form-group">
<%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>
</div>

<div class="form-group">
<%= render "devise/shared/links" %>
</div>
Expand Down
4 changes: 4 additions & 0 deletions app/views/devise/sessions/new.html.erb
Expand Up @@ -26,6 +26,10 @@
<%= f.submit "Log in", class: 'btn btn-success' %>
</div>

<div class="form-group">
<%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>
</div>

<div class="form-group">
<%= render "devise/shared/links" %>
</div>
Expand Down
5 changes: 4 additions & 1 deletion app/views/items/_item.html.erb
@@ -1,6 +1,9 @@
<div class="media" id="item-<%= item.id %>">
<div class="media-body">
<%= item.name %>
<%= link_to "", [@list, item], method: :delete, remote: true, class: 'glyphicon glyphicon-ok' %>
&nbsp;
<%= link_to "", [@list, item], method: :delete, remote: true, class: 'glyphicon glyphicon-ok' %>
&nbsp;
<%= item.days_left.to_s + " days remaining" %>
</div>
</div>
1 change: 1 addition & 0 deletions config/initializers/devise.rb
Expand Up @@ -256,4 +256,5 @@
# When using omniauth, Devise cannot automatically set Omniauth path,
# so you need to do it manually. For the users scope, it would be:
# config.omniauth_path_prefix = '/my_engine/users/auth'
config.omniauth :facebook, "331099123767127", "002f44fc522539e56c38ded3affb3a79"
end
8 changes: 4 additions & 4 deletions config/routes.rb
@@ -1,11 +1,11 @@
Rails.application.routes.draw do


devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

resources :lists, except: [:index] do
resources :items, only: [:create, :destroy]
end

devise_for :users


get 'about' => 'welcome#about'

root to: 'welcome#index'
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20150121154124_add_column_to_users.rb
@@ -0,0 +1,6 @@
class AddColumnToUsers < ActiveRecord::Migration
def change
add_column :users, :provider, :string
add_column :users, :uid, :string
end
end
14 changes: 13 additions & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,17 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150114145919) do
ActiveRecord::Schema.define(version: 20150121154124) do

create_table "identities", force: true do |t|
t.integer "user_id"
t.string "provider"
t.string "uid"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "identities", ["user_id"], name: "index_identities_on_user_id"

create_table "items", force: true do |t|
t.string "name"
Expand Down Expand Up @@ -49,6 +59,8 @@
t.string "unconfirmed_email"
t.datetime "created_at"
t.datetime "updated_at"
t.string "provider"
t.string "uid"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true
Expand Down
2 changes: 1 addition & 1 deletion db/seeds.rb
Expand Up @@ -33,7 +33,7 @@
# Create an admin user
admin = User.new(
name: 'Admin User',
email: 'jeffreykliu@gmail.com',
email: 'jeffreykliu@chicagobooth.edu',
password: 'helloworld',
)
admin.skip_confirmation!
Expand Down
7 changes: 7 additions & 0 deletions lib/tasks/todo.rake
@@ -0,0 +1,7 @@
namespace :todo do
desc "Delete items older than seven days"
task delete_items: :environment do
Item.where("created_at <= ?", Time.now - 7.days).destroy_all
end

end