在rails应用程序中自动点击电子邮件中的链接时登录用户?

我的rails应用程序中有一个User模型。 这是一个“设计”模型。 我有一个名为“user-info”的表单,一旦用户登录系统,就可以使用此表单。

我有一个admin登录,其中admin可以发送link到用户email如通过单击button填写“用户信息”。 一旦用户点击该按钮,我希望用户automatically登录系统,而无需重定向到登录页面。 并应navigateduser-info表单。

我想知道在url中发送查询字符串参数,如email =“x@gmail.com”&& form =“user_info”。 但是一旦用户点击了这个,就需要实现我的要求。

我的理解是你可以使用令牌登录或打击需要身份validation的任意页面,即使使用cURL也是如此。 如果你查看config/initializers/devise.rb ,应该有一行说:

 config.token_authentication_key = :auth_token 

无论token_authentication_key的名称是什么,都应与您在请求中作为查询或表单参数放置的名称相匹配。 您在示例中使用了authentication_token ,不确定是否更改了devise.rb以匹配。

如果你想弄清楚内部是如何工作的,我会尝试git clone git://github.com/plataformatec/devise.git并搜索你需要澄清的方法。

以下是一些示例cURL请求(我创建了一个自定义的Users :: SessionsController,它扩展了Devise :: SessionsController并覆盖了create方法来处理JSON。)

 class Users::SessionsController < Devise::SessionsController def create resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new") set_flash_message(:notice, :signed_in) if is_navigational_format? sign_in(resource_name, resource) respond_to do |format| format.html do respond_with resource, :location => redirect_location(resource_name, resource) end format.json do render :json => { :response => 'ok', :auth_token => current_user.authentication_token }.to_json, :status => :ok end end end end 

然后是我给出的cURL请求:

 curl -X POST 'http://localhost:3000/users/sign_in.json' -d 'user[email]=example@example.com&user[password]=password' -> {"response":"ok","auth_token":"ABCDE0123456789"} curl -L 'http://localhost:3000/profile?auth_token=ABCDE0123456789' -> got page that I wanted that needs authentication 

您可以使用此简单令牌身份validation gem,众所周知,生成身份validation令牌并通过电子邮件发送给您希望进行此身份validation的用户。 请按照设置说明进行操作。