使用设计重置密码问题
Posted
技术标签:
【中文标题】使用设计重置密码问题【英文标题】:Reset Password Issue with Devise 【发布时间】:2014-01-08 12:22:50 【问题描述】:我在我的 Rails 应用程序中使用 Devise,但在重置密码时遇到了问题。
当我尝试重置密码时,我会收到一封电子邮件,其中包含重置密码的链接。使用新密码填写表单后,我在 Chrome 中收到错误“网页有重定向循环”,并且在我的日志中收到以下错误:
Started GET "/users/password/edit?reset_password_token=[FILTERED]" for 127.0.0.1 at 2013-12-19 14:22:05 -0500
Processing by Devise::PasswordsController#edit as html
Parameters: "reset_password_token"=>"[FILTERED]"
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Redirected to http://localhost:3000/users/password/edit?reset_password_token=JatMT1fE-fQwsCWsEdy6
Filter chain halted as :require_no_authentication rendered or redirected
Completed 302 Found in 1.8ms (ActiveRecord: 0.4ms)
我似乎找不到任何有关如何解决此问题的信息。
user.rb
class User < ActiveRecord::Base
...
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:token_authenticatable, :confirmable, :lockable
...
end
devise.rb
Devise.setup do |config|
...
config.reset_password_within = 6.hours
...
end
routes.rb
Build::Application.routes.draw do
devise_for :users, :controllers => :registrations => :registrations
devise_scope :user do
post 'registrations' => 'registrations#create', :as => 'register'
post 'sessions' => 'sessions#create', :as => 'login'
delete 'sessions' => 'sessions#destroy', :as => 'logout'
end
resources :users do
match 'users/:id' => 'users#username'
get 'validate_username', on: :collection
get 'validate_email', on: :collection
get 'edit_profile', on: :member
get :projects, on: :member
get :favorites, on: :member
get :collections, on: :member
member do
get :follow
get :unfollow
get :following
get :followers
end
end
end
registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
skip_before_filter :verify_authenticity_token,
:if => Proc.new |c| c.request.format == 'application/json'
respond_to :json
def create
user = User.new(params[:user])
Rails.logger.info(user.inspect)
# comment out following line to re-enable confirmation
# resource.skip_confirmation!
if user.save
sign_in user
render :status => 200,
:json => :success => true,
:info => "Registered",
:data => :user => user,
:auth_token => current_user.authentication_token
else
redirect_to new_user_registration_path, notice: user.errors.full_messages[0]
Rails.logger.info(user.errors.inspect)
# render :status => :unprocessable_entity,
# :json => :success => false,
# :info => resource.errors,
# :data =>
end
end
def update
@user = User.find(current_user.id)
successfully_updated = if needs_password?(@user, params)
@user.update_with_password(params[:user])
else
# remove the virtual current_password attribute
params[:user].delete(:current_password)
@user.update_without_password(params[:user])
end
if successfully_updated
if params[:update_email]
set_flash_message :alert, :signed_up_but_unconfirmed
redirect_to after_update_path_for(@user)
else
set_flash_message :notice, :updated
sign_in @user, :bypass => true
redirect_to after_update_path_for(@user)
end
else
redirect_to :back, alert: resource.errors.full_messages[0]
end
end
private
# check if we need password to update user data
def needs_password?(user,params)
!params[:profile]
end
protected
def after_update_path_for(resource)
user_path(resource)
end
end
【问题讨论】:
控制器的外观如何? 我添加了注册控制器。 【参考方案1】:我遇到了完全相同的问题,只是我注意到重复重定向到密码重置 URL。
我认为@Sergey Sokolov 有正确的答案,虽然我修改了我的 after_sign_in_path:
def after_sign_in_path_for(resource_or_scope)
if request.referer.include? "reset_password"
root_path
else
request.referer
end
end
以便在密码重置以外的情况下,用户将返回到他或她登录的页面。这避免了用户点击电子邮件中的密码重置链接的问题。
在进行故障排除时,我还做了一些非常愚蠢的事情,当我在测试时为其他用户重置密码时,我仍然以其他用户身份登录。这会导致非常奇怪的行为。
【讨论】:
【参考方案2】:在 ApplicationController 中检查您的 after_sign_in_path_for,如果是密码编辑请求,则添加重定向到 root_url(不是 :back 或 request.env['HTTP_REFERER'])。
【讨论】:
以上是关于使用设计重置密码问题的主要内容,如果未能解决你的问题,请参考以下文章