boyjunqiang 发表于 2013-2-7 09:06:47

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

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

2.sudo gem install ruby-openid

3.sudo ruby script/plugin install open_id_authentication

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

5.rake open_id_authentication:db:create

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

7.run the migrate

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

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:
    attr_accessible :login, :email, :password, :password_confirmation, :identity_url

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

11.:
# This controller handles the login/logout function of the site.
class SessionController < ApplicationController# Be sure to include AuthenticationSystem in Application Controller instead# include AuthenticatedSystem# render new.rhtmllayout "welcome"    def newenddef create    if using_open_id?      open_id_authentication(params[:openid_url])    else      password_authentication(params[:login], params[:password])    endend    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    endend    def password_authentication(login, password)    self.current_user = User.authenticate(login, password)    if logged_in?      successful_login    else      failed_login    endend    def failed_login(message = "Authentication failed.")    flash.now[:error] = message    render :action => 'new'enddef 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] = "登录成功"endend

12.add some css:
/* 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;}
页: [1]
查看完整版本: 怎样在你的 ruby on rails 网站中加入 OpenID 登录功能