使用OAuth和OpenID进行Authlogic - DoubleRenderError

时间:2010-11-27 05:14:22

标签: ruby-on-rails oauth

我有authlogic和openid正常工作,我正在尝试将oauth集成到twitter身份验证中。我不想要一个注册和登录按钮,所以我遵循了这个例子:Implicit user creation with Authlogic and Authlogic OAuth plugin

我在初始注册和后续登录时收到DoubleRenderError,但刷新页面可以让操作成功完成。

我的users_controller创建:

  def create
        @user = User.new(params[:user])
        @user.save do |result| # LINE A
            if result
                flash[:notice] = "Account registered!"
                redirect_to account_url
            else
                unless @user.oauth_token.nil?
                    @user = User.find_by_oauth_token(@user.oauth_token)
                    unless @user.nil?
                        UserSession.create(@user)
                        flash.now[:message] = "Welcome back!"
                        redirect_to account_url        
                    else
                        redirect_back_or_default root_path
                    end
                else
                    redirect_back_or_default root_path
                end
            end
        end
    end

我的user_sessions_controller创建:

  def create
    @user_session = UserSession.new(params[:user_session])

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

有没有办法解决这个问题?感谢

1 个答案:

答案 0 :(得分:0)

在多次失败之后,以下内容似乎适用于正常的authlogic用户名/密码,OAuth与Twitter,以及至少google和yahoo的OpenID,这是我感兴趣的全部

  def create
   @user = User.new(params[:user])
   @user.save do |result| # LINE A
    if result
     flash[:notice] = "Account registered!"
     redirect_to account_url and return
    else
     if @user.oauth_token
      @user = User.find_by_oauth_token(@user.oauth_token)

       UserSession.create(@user)
       flash.now[:message] = "Welcome back!"
       redirect_to account_url and return        
      else
       flash[:notice] = "Something went awry. Perhaps the name or email is already in use."
       redirect_to register_path and return
     end

    end
   end
  end

此外,在成功和失败重定向/渲染后,我在用户控制器中添加了“并返回”更新块