forked from bofh/fetsite
Omniauth + Omniauthfacebook
für
This commit is contained in:
4
Gemfile
4
Gemfile
@@ -53,6 +53,8 @@ gem "paper_trail" , '>=2.7.0'
|
|||||||
|
|
||||||
# User management
|
# User management
|
||||||
gem "devise" ,'~>2.2.3'
|
gem "devise" ,'~>2.2.3'
|
||||||
|
gem "omniauth"
|
||||||
|
gem "omniauth-facebook"
|
||||||
|
|
||||||
# Roles for users
|
# Roles for users
|
||||||
gem "rolify"
|
gem "rolify"
|
||||||
@@ -80,4 +82,4 @@ gem "simple_calendar", "~> 0.1.9"
|
|||||||
gem 'rmagick'
|
gem 'rmagick'
|
||||||
|
|
||||||
gem 'bootstrap-addons-rails'
|
gem 'bootstrap-addons-rails'
|
||||||
gem "jquery-fileupload-rails"
|
gem "jquery-fileupload-rails"
|
||||||
|
|||||||
23
Gemfile.lock
23
Gemfile.lock
@@ -67,6 +67,8 @@ GEM
|
|||||||
factory_girl_rails (4.2.1)
|
factory_girl_rails (4.2.1)
|
||||||
factory_girl (~> 4.2.0)
|
factory_girl (~> 4.2.0)
|
||||||
railties (>= 3.0.0)
|
railties (>= 3.0.0)
|
||||||
|
faraday (0.8.8)
|
||||||
|
multipart-post (~> 1.2.0)
|
||||||
formtastic (2.2.1)
|
formtastic (2.2.1)
|
||||||
actionpack (>= 3.0)
|
actionpack (>= 3.0)
|
||||||
formtastic-bootstrap (2.0.0)
|
formtastic-bootstrap (2.0.0)
|
||||||
@@ -79,7 +81,9 @@ GEM
|
|||||||
paper_trail (~> 2)
|
paper_trail (~> 2)
|
||||||
haml (4.0.0)
|
haml (4.0.0)
|
||||||
tilt
|
tilt
|
||||||
|
hashie (2.0.5)
|
||||||
hike (1.2.1)
|
hike (1.2.1)
|
||||||
|
httpauth (0.2.0)
|
||||||
i18n (0.6.1)
|
i18n (0.6.1)
|
||||||
journey (1.0.4)
|
journey (1.0.4)
|
||||||
jquery-fileupload-rails (0.4.1)
|
jquery-fileupload-rails (0.4.1)
|
||||||
@@ -89,6 +93,8 @@ GEM
|
|||||||
railties (>= 3.0, < 5.0)
|
railties (>= 3.0, < 5.0)
|
||||||
thor (>= 0.14, < 2.0)
|
thor (>= 0.14, < 2.0)
|
||||||
json (1.7.7)
|
json (1.7.7)
|
||||||
|
jwt (0.1.8)
|
||||||
|
multi_json (>= 1.5)
|
||||||
libv8 (3.11.8.13)
|
libv8 (3.11.8.13)
|
||||||
mail (2.4.4)
|
mail (2.4.4)
|
||||||
i18n (>= 0.4.0)
|
i18n (>= 0.4.0)
|
||||||
@@ -96,6 +102,21 @@ GEM
|
|||||||
treetop (~> 1.4.8)
|
treetop (~> 1.4.8)
|
||||||
mime-types (1.21)
|
mime-types (1.21)
|
||||||
multi_json (1.5.1)
|
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)
|
orm_adapter (0.4.0)
|
||||||
paper_trail (2.7.0)
|
paper_trail (2.7.0)
|
||||||
activerecord (~> 3.0)
|
activerecord (~> 3.0)
|
||||||
@@ -199,6 +220,8 @@ DEPENDENCIES
|
|||||||
haml
|
haml
|
||||||
jquery-fileupload-rails
|
jquery-fileupload-rails
|
||||||
jquery-rails
|
jquery-rails
|
||||||
|
omniauth
|
||||||
|
omniauth-facebook
|
||||||
paper_trail (>= 2.7.0)
|
paper_trail (>= 2.7.0)
|
||||||
paperclip (~> 3.4.0)
|
paperclip (~> 3.4.0)
|
||||||
rails (= 3.2.9)
|
rails (= 3.2.9)
|
||||||
|
|||||||
15
app/controllers/users/omniauth_callbacks_controller.rb
Normal file
15
app/controllers/users/omniauth_callbacks_controller.rb
Normal 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
|
||||||
@@ -30,10 +30,29 @@ class User < ActiveRecord::Base
|
|||||||
# :token_authenticatable, :confirmable,
|
# :token_authenticatable, :confirmable,
|
||||||
# :lockable, :timeoutable and :omniauthable
|
# :lockable, :timeoutable and :omniauthable
|
||||||
devise :database_authenticatable, :registerable,:confirmable,
|
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
|
# 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
|
# 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
|
end
|
||||||
|
|||||||
1
config/initializers/omniauth.rb
Normal file
1
config/initializers/omniauth.rb
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
7
db/migrate/20130825095438_add_columns_to_users.rb
Normal file
7
db/migrate/20130825095438_add_columns_to_users.rb
Normal 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
|
||||||
Reference in New Issue
Block a user