百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

authlogic_oauth

> 编程语言
开源

Authlogic OAuth 是 Authlogic 库的一个扩展,用于添加 OAuth 支持。OAuth 可用于允许用户使用其 Twitter 凭据进行登录。

166 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Authlogic OAuth 是 Authlogic 库的一个扩展,用于添加 OAuth 支持。OAuth 可用于允许用户使用其 Twitter 凭据进行登录。

= 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: http://github.com/binarylogic/authlogic
  • OAuth Example Project: http://github.com/jrallison/authlogic_example/tree/with-oauth
  • Live example with Twitter: http://authlogic-oauth.heroku.com

== Install and use

=== 1. Install Authlogic and setup your application

  • Authlogic: http://github.com/binarylogic/authlogic
  • Authlogic Example: http://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 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 "http://twitter.com", :authorize_url => "http://twitter.com/oauth/authenticate" }) end

end

=== 6. Add login and register buttons to your views

"Register with Twitter" %> "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.

Issues· 13 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言