And*_*rew 65 facebook ruby-on-rails devise
我有一个使用Devise进行身份验证的Rails 3应用程序.现在我需要允许某人使用他们的Facebook帐户登录.我认为这叫做Facebook Connect,但我也听过Facebook Graph API一词,所以我不确定我要求哪一个.
为了将Facebook Connect与Devise集成,我需要做些什么?
这个问题现在很老了.一年前,Devise v1.2引入了OmniAuth支持.现在Devise处于v2.1(截至本文撰写时),使用OmniAuth更加容易.这是一个来自Devise wiki的精彩教程,它使用omniauth-facebook带有Devise 的gem来允许使用Facebook登录.
另外,请查看有关注册应用程序和使用Facebook Graph API的精彩教程.
Hug*_*ugo 54
我检查了设计github页面,看看他们在做什么.该项目进展非常快,因此它们支持facebook连接等.查看OAuth2部分.他们使用github作为例子,但对于facebook来说它们是相同的,他们提到了差异.我认为这是要走的路,设计的第三方宝石不会像设计或轨道一样快速移动.干杯.
哎呀这里是链接http://github.com/plataformatec/devise
编辑
当然我在这里做了很少的编码,大多数是默认的,所以这里是:
创建一个新的应用程序并将这些宝石添加到gemfile中.
gem 'devise', :git => 'git://github.com/plataformatec/devise.git'
gem 'oauth2', :git => 'git://github.com/intridea/oauth2.git'
Run Code Online (Sandbox Code Playgroud)
运行bundle install,然后这些命令将使您获得基本的用户身份验证模型.
rails generate devise:install
rails generate devise User
Run Code Online (Sandbox Code Playgroud)
在config/initializers/devise.rb中取消注释/修改这些.看看最后一段,你从Facebook获取app_key和秘密.
config.oauth :facebook, 'app_key', 'secret',
:site => 'https://graph.facebook.com',
:authorize_path => '/oauth/authorize',
:access_token_path => '/oauth/access_token'
Run Code Online (Sandbox Code Playgroud)
这应该是您的用户模型.
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable, :timeoutable and :oauthable
devise :database_authenticatable, :oauthable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
# Get the user email info from Facebook for sign up
# You'll have to figure this part out from the json you get back
data = ActiveSupport::JSON.decode(access_token)
if user = User.find_by_email(data["email"])
user
else
# Create an user with a stub password.
User.create!(:name => data["name"], :email => data["email"], :password => Devise.friendly_token)
end
end
end
Run Code Online (Sandbox Code Playgroud)
Devise使用root:to =>"something#here"所以我创建了一个带有索引操作的家庭控制器,并使用它来根据应用程序.但没关系.我把它放在layout/application.html.erb中,这样我就有了基本的sign_n sign_out路由.
<span>
<%- if user_signed_in? %>
<%= "Signed in as #{current_user.full_name}. Not you?" %>
<%= link_to 'Sign out', destroy_user_session_path %>
<%- else %>
<%= link_to 'Sign in', new_user_session_path %>
<%- end %>
</span>
Run Code Online (Sandbox Code Playgroud)
设计几乎照顾我们的一切.你需要做的是从facebook获取你的app_key和秘密(在devise.rb配置文件中使用).这个链接应该让你去.http://developers.facebook.com/setup
刚刚使用Hugo解决方案几乎没有问题.这是我必须使用的User.rb代码:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable, :timeoutable and :oauthable
devise :database_authenticatable, :oauthable
# Setup accessible (or protected) attributes for your model
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
# Get the user email info from Facebook for sign up
# You'll have to figure this part out from the json you get back
data = ActiveSupport::JSON.decode(access_token.get('https://graph.facebook.com/me?'))
logger.info("received from Facebook: #{data.inspect}")
if user = User.find_by_email(data["email"])
user
else
# Create an user with a stub password.
User.create!(:name => data["name"], :email => data["email"], :password => Devise.friendly_token)
end
end
end
Run Code Online (Sandbox Code Playgroud)
这段代码中的内容发生了变化:
| 归档时间: |
|
| 查看次数: |
39073 次 |
| 最近记录: |