gpt4 book ai didi

ruby-on-rails - 使用Devise重置密码问题

转载 作者:行者123 更新时间:2023-12-04 07:36:11 26 4
gpt4 key购买 nike

我在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

最佳答案

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

关于ruby-on-rails - 使用Devise重置密码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20690071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com