使用Authlogic和Authlogic OAuth插件创建隐式用户

时间:2022-08-19 16:47:47

I'm trying to write a simple OAuth consumer app in Rails. I'm using Authlogic for handling authentication and the Authlogic OAuth plugin to do the oauth thing.

我想在Rails上写一个简单的OAuth消费者应用。我使用Authlogic来处理身份验证和Authlogic OAuth插件来完成OAuth的工作。

The oauth plugin provides a couple of helpers to render the sign in button: oauth_login_button and oauth_register_button. Together with the Authlogic logics and the plugin's request filters these two buttons somehow create the session/user.

oauth插件提供了两个助手来呈现按钮中的签名:oauth_login_button和oauth_register_button。与Authlogic逻辑以及插件的请求一起过滤这两个按钮以某种方式创建会话/用户。

What happens next is as follows: - if I use the oauth_login_button helper, then the session object fails to save as there's no such user locally. - if I use the oauth_register_button helper, then, on any login after the first one, Rails complains that the token has been taken already... that means it can't create the second copy for the same user, which is right.

接下来发生的是:-如果我使用oauth_login_button helper,那么会话对象将无法保存,因为本地没有这样的用户。-如果我使用oauth_register_button helper,那么,在第一个登录之后的任何登录中,Rails都会抱怨已经获取了令牌……这意味着它不能为相同的用户创建第二个副本,这是正确的。

The issue is: I don't want to have BOTH Register AND Login buttons on my site.

问题是:我不想在我的站点上同时有注册和登录按钮。

On the user side, what I want to achieve is a single button on the start page, saying smth. like "Sign In with Twitter", which the user must click to proceed to inner pages of the site.

在用户端,我想实现的是开始页面上的一个按钮,上面写着smth。比如“登录Twitter”,用户必须点击它才能进入网站的内部页面。

On the server side, I want to implicitly create the local user account, if the user is a first time visitor to my site.

在服务器端,如果用户是我的站点的第一个访问者,我希望隐式地创建本地用户帐户。

Any hints on how to do this?

有什么建议吗?

All the samples on Authlogic+OAuth I was able to find don't seem to care about having only a single button for sign in. :(

我能找到的Authlogic+OAuth的所有样本似乎都不关心是否只有一个按钮来登录。:(

1 个解决方案

#1


12  

Seems like I'm going to answer the question myself.

看来我要自己回答这个问题了。

I use the following code to generate the Sign In button (in HAML):

我使用下面的代码来生成登录按钮(在HAML中):

- form_tag({:controller => "users", :action => "create"}, {:method => "post"}) do
  = oauth_register_button :value => "Sign In with Twitter"

and then I simply create the user's session object in the create method of the UsersController class, if the user already exists:

然后,如果用户已经存在,我只需在UsersController类的create方法中创建用户的会话对象:

def create
  @user = User.new(params[:user])
  @user.save do |result| # LINE A
    if result
      flash[:notice] = "Account registered!"
      redirect_to some_inner_path
    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 some_inner_path        
        else
          redirect_back_or_default root_path
        end
      else
        redirect_back_or_default root_path
      end
    end
  end
end

If the user is a first time visitor, then the user object is successfully saved in the LINE A. And if it's not and there's an oauth token available, then we try to fetch the user from the DB and log him/her in.

如果用户是第一次访问,那么用户对象就会成功地保存在a行中。如果没有,并且有一个oauth令牌可用,那么我们尝试从数据库中获取用户并将其登录。

#1


12  

Seems like I'm going to answer the question myself.

看来我要自己回答这个问题了。

I use the following code to generate the Sign In button (in HAML):

我使用下面的代码来生成登录按钮(在HAML中):

- form_tag({:controller => "users", :action => "create"}, {:method => "post"}) do
  = oauth_register_button :value => "Sign In with Twitter"

and then I simply create the user's session object in the create method of the UsersController class, if the user already exists:

然后,如果用户已经存在,我只需在UsersController类的create方法中创建用户的会话对象:

def create
  @user = User.new(params[:user])
  @user.save do |result| # LINE A
    if result
      flash[:notice] = "Account registered!"
      redirect_to some_inner_path
    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 some_inner_path        
        else
          redirect_back_or_default root_path
        end
      else
        redirect_back_or_default root_path
      end
    end
  end
end

If the user is a first time visitor, then the user object is successfully saved in the LINE A. And if it's not and there's an oauth token available, then we try to fetch the user from the DB and log him/her in.

如果用户是第一次访问,那么用户对象就会成功地保存在a行中。如果没有,并且有一个oauth令牌可用,那么我们尝试从数据库中获取用户并将其登录。