Rails 3 using Devise: How to allow someone to log in using their Facebook account?

后端 未结 7 2086
逝去的感伤
逝去的感伤 2020-12-07 07:18

I have a Rails 3 application using Devise for authentication. Now I need to allow someone to log in using their Facebook account. I think this is called Facebook Connect, bu

相关标签:
7条回答
  • 2020-12-07 07:45

    I checked the devise github page to see what they were up to. That project is moving pretty fast and as it happens they have support for facebook connect amongst other things. Check out the section on OAuth2. They use github as an example but it would be the same thing for facebook and they mention differences. I think this is the way to go, third party gems for devise don't move as fast as devise or rails do. Cheers.

    Oops here's the link http://github.com/plataformatec/devise

    Edit

    Of course I did very little coding here mostly went with the default, so here goes:

    Create a new app and add these gems to the gemfile.

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

    Run bundle install, then these commands gets you going with a basic User authentication model.

    rails generate devise:install
    rails generate devise User
    

    In config/initializers/devise.rb uncomment/modify these. Look at the last paragraph as to where you get app_key and secret from facebook.

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

    This should be your user model.

    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 uses a root :to => "something#here" so I created a home controller with a index action and used that to root the application. But nevermind that. I put that in layout/application.html.erb so that I had basic sign_n sign_out routes.

    <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 pretty much takes care of everything else for us. What you do need to do though is get your app_key and secret from facebook (used in devise.rb config file). This link should get you going. http://developers.facebook.com/setup

    0 讨论(0)
  • 2020-12-07 07:48

    This blog post did it for me. Give it a look.

    0 讨论(0)
  • 2020-12-07 07:50

    Devise 1.2 now comes with facebook login support using omniauth and works with Rails 3.0. Check out the wiki entry.

    0 讨论(0)
  • 2020-12-07 07:50

    Just used Hugo solution with almost no problem. Here is the User.rb code I had to use :

    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
    

    The things changed in this code :

    • name is in attr_accessible (don't forget to add a name field to user)
    • changed JSON decoding
    0 讨论(0)
  • 2020-12-07 07:51

    In my app, I use omniauth, which I think came out a bit after this question was answered.

    https://github.com/intridea/omniauth

    0 讨论(0)
  • 2020-12-07 07:52

    http://github.com/grimen/devise_facebook_connectable

    This gem on github is quite straightforward. Worth a shot!

    0 讨论(0)
提交回复
热议问题