config.gem "authlogic" config.gem "oauth2" $ sudo rake gems:install
$ script/plugin install git://github.com/andyhite/authlogic_oauth2.git
This plugin will be soon be packaged as a gem.
For information about how to set up and configure authlogic, please consult the authlogic README (github.com/binarylogic/authlogic)
class AddOauth2FieldsToUser < ActiveRecord::Migration def self.up add_column :users, :oauth2_token, :string add_index :users, :oauth2_token end def self.down remove_column :users, :oauth2_token end end
IMPORTANT: make sure that you allow null values for email, crypted_password, and password_salt if they aren’t required for OAuth2 users.
The oauth2_client method should return an OAuth2::Client that is configured for your OAuth2 provider.
The oauth2_scope method should return a string representing the extended permission you need to request from the OAuth2 provider.
Here’s an example for Facebook:
class UserSession < Authlogic::Session::Base def self.oauth2_client OAuth2::Client.new("CLIENT_ID", "SECRET_KEY", :site => "https://graph.facebook.com") end def self.oauth2_scope 'email,user_birthday' end end
We need to redirect the user to their oauth2 provider so they can authenticate and then pick things back up when they’re returned, so any calls to User#save or UserSession#save need to be updated to the following format:
@user.save do |result| if result # Do something else # Do something else end end
and
@user_session.save do |result| if result # Do something else # Do something else end end
In file app/views/user_sessions/new.html.erb:
<% form_for @user_session, :url => user_session_path do |f| %> # All your other form stuff goes here, if you need it. <%= oauth2_login_button :value => "Login using Facebook" %> <% end %>
In file app/views/users/new.html.erb:
<% form_for @user, :url => account_path do |f| %> # All your other form stuff goes here, if you need it. <%= oauth2_register_button :value => "Register using Facebook" %> <% end %>
If you followed these steps correctly, then you should be able to register and login using OAuth2.