怎样在你的 ruby on rails 网站中加入 OpenID 登录功能

my OS is ubuntu7.10:
1.cd to the project directory

2.[code]sudo gem install ruby-openid[/code]

3.[code]sudo ruby script/plugin install open_id_authentication[/code]

4.rake -T open_id (to see the rake command)

5.[code]rake open_id_authentication:db:create[/code]

6.in the open-id migrate add these lines:
[code]add_column :users, :identity_url, :string [/code](in self.up)
[code]remove_column :users, :identity_url[/code] (in self.down)

7.run the migrate

8.add a line in routes.rb:
[code] map.open_id_complete 'session', :controller => "session", :action => "create",:requirements => { :method => :get }[/code]

VERY VERY IMPORTENT!!!
make sure the map.open_id_complete line above the map.resource :session line;(IT waste lots of my time!!!)

9:add a :identity_url in user.rb and it will look like:
[code] attr_accessible :login, :email, :password, :password_confirmation, :identity_url[/code]

10.add some line in session/new.rhtml:
[code] <p>
<label for="openid_url">OpenID URL</label><br/>
<%=text_field_tag "openid_url"%>
</p>[/code]

11.:
# This controller handles the login/logout function of the site.
[code]class SessionController < ApplicationController
# Be sure to include AuthenticationSystem in Application Controller instead
# include AuthenticatedSystem

# render new.rhtml
layout "welcome"

def new
end

def create
if using_open_id?
open_id_authentication(params[:openid_url])
else
password_authentication(params[:login], params[:password])
end
end

def destroy
self.current_user.forget_me if logged_in?
cookies.delete :auth_token
reset_session
flash[:notice] = "你已经登录了."
redirect_back_or_default('/')
end

protected

def open_id_authentication(openid_url)
authenticate_with_open_id(openid_url, :required => [:nickname, :email]) do |result, identity_url, registration|
if result.successful?
@user = User.find_or_initialize_by_identity_url(identity_url)
if @user.new_record?
@user.login = registration['nickname']
@user.email = registration['email']
@user.save(false)
end
self.current_user = @user
successful_login
else
failed_login result.message
end
end
end

def password_authentication(login, password)
self.current_user = User.authenticate(login, password)
if logged_in?
successful_login
else
failed_login
end
end

def failed_login(message = "Authentication failed.")
flash.now[:error] = message
render :action => 'new'
end

def successful_login
if params[:remember_me] == "1"
self.current_user.remember_me
cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
end
redirect_back_or_default('/tools')
flash[:notice] = "登录成功"
end


end[/code]

12.add some css:
[code]/* embeds the openid image in the text field */
input#openid_url {
background: url(http://openid.net/login-bg.gif) no-repeat;
background-color: #fff;
background-position: 0 50%;
color: #000;
padding-left: 18px;
} [/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断路器保护灵敏度校验整改及剩余电流监测试点应用站用交流系统断

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值