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' %> +