使用 Authlogic 和 Authlogic OAuth 插件隐式创建用户 [英] Implicit user creation with Authlogic and Authlogic OAuth plugin

查看:66
本文介绍了使用 Authlogic 和 Authlogic OAuth 插件隐式创建用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Rails 中编写一个简单的 OAuth 消费者应用程序.我使用 Authlogic 处理身份验证,使用 Authlogic OAuth 插件来执行 oauth 操作.

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.

oauth 插件提供了几个帮助器来呈现登录按钮:oauth_login_button 和 oauth_register_button.连同 Authlogic 逻辑和插件的请求过滤器,这两个按钮以某种方式创建会话/用户.

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_login_button 助手,则会话对象无法保存,因为本地没有这样的用户.- 如果我使用 oauth_register_button 助手,那么在第一次登录之后的任何登录时,Rails 都会抱怨令牌已被占用......这意味着它无法为同一用户创建第二个副本,这是正确的.

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.

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

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

在用户方面,我想要实现的是开始页面上的单个按钮,说 smth.例如使用 Twitter 登录",用户必须点击它才能进入网站的内页.

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.

在服务器端,如果用户是我网站的第一次访问者,我想隐式创建本地用户帐户.

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?

我能找到的 Authlogic+OAuth 上的所有示例似乎都不关心只有一个登录按钮.:(

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. :(

推荐答案

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

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

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

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

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

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

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

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

如果用户是第一次访问,则用户对象成功保存在 A 行中.如果不是,并且有可用的 oauth 令牌,那么我们尝试从数据库中获取用户并记录他/她

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.

这篇关于使用 Authlogic 和 Authlogic OAuth 插件隐式创建用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆