diff --git a/Gemfile b/Gemfile index 20f914c..cd9bbc7 100644 --- a/Gemfile +++ b/Gemfile @@ -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 \ No newline at end of file +gem 'faker' #Faker, fake data +gem 'omniauth' #Omniauth, various authentication +gem 'omniauth-facebook' #OmniAuth Facebook, FB authentication \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index dbba35b..17eae7c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -75,6 +78,7 @@ 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) @@ -82,8 +86,26 @@ GEM 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) @@ -186,6 +208,8 @@ DEPENDENCIES figaro jbuilder (~> 2.0) jquery-rails + omniauth + omniauth-facebook pg pry-rails rails (= 4.1.7) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9d59fa9..c8f5327 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 \ No newline at end of file diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb new file mode 100644 index 0000000..067985b --- /dev/null +++ b/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 \ No newline at end of file diff --git a/app/models/item.rb b/app/models/item.rb index 857cd63..aff9317 100644 --- a/app/models/item.rb +++ b/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 \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index adaf2bb..02e0f7c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 \ No newline at end of file diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index b932e0c..d9ef238 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -32,6 +32,10 @@ <%= f.submit "Sign up", class: 'btn btn-success' %> +
+ <%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %> +
+
<%= render "devise/shared/links" %>
diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index 59a3437..a4a1466 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -26,6 +26,10 @@ <%= f.submit "Log in", class: 'btn btn-success' %> +
+ <%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %> +
+
<%= render "devise/shared/links" %>
diff --git a/app/views/items/_item.html.erb b/app/views/items/_item.html.erb index f93df0c..220287c 100644 --- a/app/views/items/_item.html.erb +++ b/app/views/items/_item.html.erb @@ -1,6 +1,9 @@
<%= item.name %> - <%= link_to "", [@list, item], method: :delete, remote: true, class: 'glyphicon glyphicon-ok' %> +   + <%= link_to "", [@list, item], method: :delete, remote: true, class: 'glyphicon glyphicon-ok' %> +   + <%= item.days_left.to_s + " days remaining" %>
\ No newline at end of file diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 2d17182..b350c60 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index a1d6386..4650c46 100644 --- a/config/routes.rb +++ b/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' diff --git a/db/migrate/20150121154124_add_column_to_users.rb b/db/migrate/20150121154124_add_column_to_users.rb new file mode 100644 index 0000000..46349b7 --- /dev/null +++ b/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 diff --git a/db/schema.rb b/db/schema.rb index 651275b..6c63c95 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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 diff --git a/db/seeds.rb b/db/seeds.rb index 4e9386a..2c1bffd 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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! diff --git a/lib/tasks/todo.rake b/lib/tasks/todo.rake new file mode 100644 index 0000000..3253b83 --- /dev/null +++ b/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 \ No newline at end of file