github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

jrallison / authlogic_oauth

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 121
    • 13
  • Source
  • Commits
  • Network (13)
  • Issues (10)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Authlogic OAuth is an extension of the Authlogic library to add OAuth support. OAuth can be used to allow users to login with their Twitter credentials. — Read more

  cancel

authlogic-oauth.heroku.com

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Fixed an exception when trying to save models outside of the controller 
process (e.g. in IRB) Comment
bjeanes (author)
Tue Oct 20 20:32:30 -0700 2009
jrallison (committer)
Tue Oct 20 20:39:49 -0700 2009
commit  541626973b317b75d3645fb5108d0006322adaf2
tree    4646d7994d706ea420a1e799a876814fbb01848e
parent  a6c2e8dd0701cca664b89573c24f2ead8e009b7d
authlogic_oauth /
name age
history
message
file .gitignore Thu Jun 04 13:26:31 -0700 2009 Adding gitignore [jrallison]
file CHANGELOG.rdoc Sun Aug 02 13:15:58 -0700 2009 Updating version [John Allison]
file MIT-LICENSE Fri May 29 23:35:47 -0700 2009 Initial Commit [jrallison]
file Manifest.txt Sat Jun 27 16:23:32 -0700 2009 Adding oauth_callback_filter to the manifest [John Allison]
file README.rdoc Sat Jun 27 10:15:41 -0700 2009 Fixing typo in the readme =/ [jrallison]
file Rakefile Sun May 31 14:06:08 -0700 2009 Updating Hoe names [jrallison]
file authlogic_oauth.gemspec Tue Oct 13 20:00:56 -0700 2009 Added gemspec so github will like me [ggoodale]
file init.rb Fri May 29 23:35:47 -0700 2009 Initial Commit [jrallison]
directory lib/ Tue Oct 20 20:39:49 -0700 2009 Fixed an exception when trying to save models o... [bjeanes]
directory rails/ Fri Jun 26 23:44:17 -0700 2009 Requiring callback filter [jrallison]
directory test/ Fri Jun 26 19:44:10 -0700 2009 Working on test coverage ... still a long way t... [jrallison]
README.rdoc

Authlogic OAuth

Authlogic OAuth is an extension of the Authlogic library to add OAuth support. One use case for authentication with OAuth is allowing users to log in with their Twitter credentials.

Helpful links

  • Authlogic: github.com/binarylogic/authlogic
  • OAuth Example Project: github.com/jrallison/authlogic_example/tree/with-oauth
  • Live example with Twitter: authlogic-oauth.heroku.com

Install and use

1. Install Authlogic and setup your application

  • Authlogic: github.com/binarylogic/authlogic
  • Authlogic Example: github.com/binarylogic/authlogic_example

2. Install OAuth and Authlogic_Oauth

  $ sudo gem install oauth
  $ sudo gem install authlogic-oauth

Now add the gem dependencies in your config:

  config.gem "oauth"
  config.gem "authlogic-oauth", :lib => "authlogic_oauth"

Or for older version of rails, install it as a plugin:

  $ script/plugin install git://github.com/jrallison/authlogic_oauth.git

3. Make some simple changes to your database:

  class AddUsersOauthFields < ActiveRecord::Migration
    def self.up
      add_column :users, :oauth_token, :string
      add_column :users, :oauth_secret, :string
      add_index :users, :oauth_token

      change_column :users, :login, :string, :default => nil, :null => true
      change_column :users, :crypted_password, :string, :default => nil, :null => true
      change_column :users, :password_salt, :string, :default => nil, :null => true
    end

    def self.down
      remove_column :users, :oauth_token
      remove_column :users, :oauth_secret

      [:login, :crypted_password, :password_salt].each do |field|
        User.all(:conditions => "#{field} is NULL").each { |user| user.update_attribute(field, "") if user.send(field).nil? }
        change_column :users, field, :string, :default => "", :null => false
      end
    end
  end

4. Make sure you save your objects properly

You only need to save your objects this way if you want the user to authenticate with their OAuth provider.

That being said, you probably want to do this in your controllers. You should do this for BOTH your User objects and UserSession objects (assuming you are authenticating users). It should look something like this:

  @user_session.save do |result|
    if result
      flash[:notice] = "Login successful!"
      redirect_back_or_default account_url
    else
      render :action => :new
    end
  end

You should save your @user objects this way as well, because you also want the user to authenticate with OAuth.

Notice we are saving with a block. Why? Because we need to redirect the user to their OAuth provider so that they can authenticate. When we do this, we don’t want to execute that block of code, because if we do, we will get a DoubleRender error. This lets us skip that entire block and send the user along their way without any problems.

5. Define the oauth_consumer class method on your UserSession model

The oauth_consumer should return an OAuth::Consumer which is configured for your OAuth provider. Here’s an example for Twitter:

  class UserSession < Authlogic::Session::Base

    def self.oauth_consumer
      OAuth::Consumer.new("TOKEN", "SECRET",
      { :site=>"http://twitter.com",
        :authorize_url => "http://twitter.com/oauth/authenticate" })
    end

  end

6. Add login and register buttons to your views

  <%= oauth_register_button :value => "Register with Twitter" %>
  <%= oauth_login_button :value => "Login with Twitter" %>

That’s it! The rest is taken care of for you.

Here are some next steps for the plugin.

  1. Safe OAuth error handling.
  2. Remove oauth request from the Rails request cycle.
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server