使用设计重置密码问题



我正在我的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)

我似乎找不到有关如何解决此问题的任何信息。

用户.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

路线.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

在 ApplicationController 中检查您的after_sign_in_path_for,如果它是密码编辑请求,请添加重定向到root_url(不是 :back 或 request.env['HTTP_REFERER'])。

我遇到了完全相同的问题,只是我注意到重复重定向到密码重置 URL。

我认为@Sergey索科洛夫有正确的答案,尽管我修改了我的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

这样,在密码重置以外的情况下,用户将返回到他或她登录的页面。 这样可以避免用户在电子邮件中点击密码重置链接的问题。

我在故障排除时也做了一些非常愚蠢的事情,并且在测试时为其他用户重置密码时保持以其他用户身份登录。 这会导致非常奇怪的行为。

最新更新