From c592bf9d9784a8f7d5905d0b0b6b173d1666bf15 Mon Sep 17 00:00:00 2001 From: Andreas Stephanides Date: Sun, 25 Aug 2013 13:08:39 +0200 Subject: [PATCH] Omniauth + Omniauthfacebook MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit für --- Gemfile | 4 ++- Gemfile.lock | 23 +++++++++++++++++ .../users/omniauth_callbacks_controller.rb | 15 +++++++++++ app/models/user.rb | 25 ++++++++++++++++--- config/initializers/omniauth.rb | 1 + .../20130825095438_add_columns_to_users.rb | 7 ++++++ 6 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 app/controllers/users/omniauth_callbacks_controller.rb create mode 100644 config/initializers/omniauth.rb create mode 100644 db/migrate/20130825095438_add_columns_to_users.rb diff --git a/Gemfile b/Gemfile index 7f63ecb..0ad052a 100755 --- a/Gemfile +++ b/Gemfile @@ -53,6 +53,8 @@ gem "paper_trail" , '>=2.7.0' # User management gem "devise" ,'~>2.2.3' +gem "omniauth" +gem "omniauth-facebook" # Roles for users gem "rolify" @@ -80,4 +82,4 @@ gem "simple_calendar", "~> 0.1.9" gem 'rmagick' gem 'bootstrap-addons-rails' -gem "jquery-fileupload-rails" \ No newline at end of file +gem "jquery-fileupload-rails" diff --git a/Gemfile.lock b/Gemfile.lock index 797788d..e7fc484 100755 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -67,6 +67,8 @@ GEM factory_girl_rails (4.2.1) factory_girl (~> 4.2.0) railties (>= 3.0.0) + faraday (0.8.8) + multipart-post (~> 1.2.0) formtastic (2.2.1) actionpack (>= 3.0) formtastic-bootstrap (2.0.0) @@ -79,7 +81,9 @@ GEM paper_trail (~> 2) haml (4.0.0) tilt + hashie (2.0.5) hike (1.2.1) + httpauth (0.2.0) i18n (0.6.1) journey (1.0.4) jquery-fileupload-rails (0.4.1) @@ -89,6 +93,8 @@ GEM railties (>= 3.0, < 5.0) thor (>= 0.14, < 2.0) json (1.7.7) + jwt (0.1.8) + multi_json (>= 1.5) libv8 (3.11.8.13) mail (2.4.4) i18n (>= 0.4.0) @@ -96,6 +102,21 @@ GEM treetop (~> 1.4.8) mime-types (1.21) multi_json (1.5.1) + multipart-post (1.2.0) + oauth2 (0.8.1) + faraday (~> 0.8) + httpauth (~> 0.1) + jwt (~> 0.1.4) + multi_json (~> 1.0) + rack (~> 1.2) + omniauth (1.1.4) + hashie (>= 1.2, < 3) + rack + omniauth-facebook (1.4.1) + omniauth-oauth2 (~> 1.1.0) + omniauth-oauth2 (1.1.1) + oauth2 (~> 0.8.0) + omniauth (~> 1.0) orm_adapter (0.4.0) paper_trail (2.7.0) activerecord (~> 3.0) @@ -199,6 +220,8 @@ DEPENDENCIES haml jquery-fileupload-rails jquery-rails + omniauth + omniauth-facebook paper_trail (>= 2.7.0) paperclip (~> 3.4.0) rails (= 3.2.9) diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb new file mode 100644 index 0000000..a1abe98 --- /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.find_for_facebook_oauth(request.env["omniauth.auth"], current_user) + + if @user.persisted? + sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated + 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 diff --git a/app/models/user.rb b/app/models/user.rb index 3995a3d..92719ab 100755 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -30,10 +30,29 @@ class User < ActiveRecord::Base # :token_authenticatable, :confirmable, # :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable,:confirmable, - :recoverable, :rememberable, :trackable, :validatable + :recoverable, :rememberable, :trackable, :validatable,:omniauthable, :omniauth_providers => [:facebook] # Setup accessible (or protected) attributes for your model - attr_accessible :email, :password, :password_confirmation, :remember_me + attr_accessible :email, :password, :password_confirmation, :remember_me, :provider, :uid, :name # attr_accessible :title, :body - + def self.find_for_facebook_oauth(auth, signed_in_resource=nil) + user = User.where(:provider => auth.provider, :uid => auth.uid).first + unless user + user = User.create(name:auth.extra.raw_info.name, + provider:auth.provider, + uid:auth.uid, + email:auth.info.email, + password:Devise.friendly_token[0,20] + ) + end + user + 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 diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/config/initializers/omniauth.rb @@ -0,0 +1 @@ + diff --git a/db/migrate/20130825095438_add_columns_to_users.rb b/db/migrate/20130825095438_add_columns_to_users.rb new file mode 100644 index 0000000..989f32e --- /dev/null +++ b/db/migrate/20130825095438_add_columns_to_users.rb @@ -0,0 +1,7 @@ +class AddColumnsToUsers < ActiveRecord::Migration + def change + add_column :users, :provider, :string + add_column :users, :uid, :string + add_column :users, :name, :string + end +end