Omniauth + Omniauthfacebook

für
This commit is contained in:
Andreas Stephanides
2013-08-25 13:08:39 +02:00
parent 79e4b71df3
commit c592bf9d97
6 changed files with 71 additions and 4 deletions

View File

@@ -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"

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -0,0 +1 @@

View File

@@ -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