Rails 3使用devise:如何让别人使用他们的Facebook帐户login?

我有一个使用Devise进行authentication的Rails 3应用程序。 现在我需要允许某人使用他们的Facebook帐户login。 我认为这被称为Facebook的连接,但我也听说过Facebook Graph API,所以我不知道我要求哪一个。

为了将Facebook Connect与Devise集成,我需要做些什么?

解:

这个问题现在很老了。 一年前,Devise v1.2引入了OmniAuth支持。 现在Devise在v2.1(截至撰写本文),使用OmniAuth更容易。 这是Devise wiki上的一个很棒的教程, 使用omniauth-facebook gem和Devise来允许使用Facebook进行login 。

另外看看这个关于注册你的应用程序和使用Facebook Graph API的优秀教程。

Devise 1.2现在提供了使用omniauth的Facebooklogin支持,并且可以与Rails 3.0一起使用。 检查维基条目 。

我检查了devisegithub页面,看看他们是什么。 该项目正在快速发展,正如它们支持Facebook连接一样。 查看OAuth2的部分。 他们使用github作为例子,但对facebook来说也是一样,他们提到了不同之处。 我认为这是要走的路,第三方devise的gem不要像devise或铁轨那样快。 干杯。

糟糕的是这里的链接http://github.com/plataformatec/devise

编辑

当然,我编写的代码很less,大部分都是默认的,所以这里是:

创build一个新的应用程序,并将这些gem添加到gemfile。

 gem 'devise', :git => 'git://github.com/plataformatec/devise.git' gem 'oauth2', :git => 'git://github.com/intridea/oauth2.git' 

运行软件包安装,然后这些命令可以使用基本的用户身份validation模型。

 rails generate devise:install rails generate devise User 

在config / initializers / devise.rb中取消注释/修改这些。 看看你从哪里得到app_key和facebook的最后一段。

 config.oauth :facebook, 'app_key', 'secret', :site => 'https://graph.facebook.com', :authorize_path => '/oauth/authorize', :access_token_path => '/oauth/access_token' 

这应该是你的用户模型。

 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 

Devise使用root:to =>“something#here”,所以我创build了一个带有索引操作的家庭控制器,并使用它来对应用程序进行根。 但是没关系。 我把它放在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> 

多为我们devise一切。 你需要做的是从Facebook获取你的app_key和secret(用在devise.rbconfiguration文件中)。 这个链接应该让你去。 http://developers.facebook.com/setup

在我的应用程序中,我使用了omniauth,在回答这个问题之后,我想它会出来。

https://github.com/intridea/omniauth

这篇博文是为我做的。 看一看。

刚刚使用Hugo解决scheme几乎没有问题。 这是我必须使用的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 

在这个代码中改变的东西:

  • 名字在attr_accessible(不要忘记给用户添加一个名称字段)
  • 改变了JSON解码

http://github.com/grimen/devise_facebook_connectable

github上的这个gem非常简单。 值得一试!

这是一个与Devise + Twitter + Facebook + Linkedin + Google + Github集成的小型应用程序。 所有在一个地方。

你可以在这里find源代码和演示