加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – Devise – 用户编辑电子邮件后确认

发布时间:2020-12-16 20:24:38 所属栏目:百科 来源:网络整理
导读:我一直在想这个2天.我通过电子邮件确认确认用户帐户(通过Devise).我终于得到了所有的工作,但总的来说,验证一个人拥有他们自己拥有的电子邮件.因此,当用户更改电子邮件时,我需要重新确认. 为了做到这一点,我创建了registrations_controller,并已经写了更新方
我一直在想这个2天.我通过电子邮件确认确认用户帐户(通过Devise).我终于得到了所有的工作,但总的来说,验证一个人拥有他们自己拥有的电子邮件.因此,当用户更改电子邮件时,我需要重新确认.

为了做到这一点,我创建了registrations_controller,并已经写了更新方法.主要是基于Devise的,但是我查看是否需要根据更新发送确认.

# registrations_controller.rb
def update
  self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)

  send_confirmation = false
  if params[:user][:email] != resource.email
    send_confirmation = true
  end

  if resource.update_with_password(params[resource_name])
    set_flash_message :notice,:updated if is_navigational_format?
    sign_in resource_name,resource,:bypass => true

    if send_confirmation
      resource.update_attributes(:confirmed_at => nil,:confirmation_sent_at => nil)
      resource.send_confirmation_instructions        
    end

    respond_with resource,:location => after_update_path_for(resource)
  else
    clean_up_passwords(resource)
    respond_with_navigational(resource){ render_with_scope :edit }
  end
end

我的问题是我不知道在哪里可以改变被重定向到哪里.我有一个页面说明“发送电子邮件以确认您的电子邮件”.但是,如果用户点击“更新帐户”后,如果我尝试将其发送到send_confirmation_instructions,那么他们会被注销(推送到登录屏幕),然后当他们通过电子邮件确认帐户时,他们被定向到我想要的页面给他们看.

我有一个自定义的Warden策略,其中有一些投入,我也写过了Devise提供的过滤器:

# registrations_controller.rb
def authenticate_scope!
  puts "RegistrationsController :: authenticate_scope!"
  puts "action : #{params[:action]}"

  super
end

所以看起来它正在试图验证用户.日志如下:

...
Redirected to http://localhost:3000/users/edit
Completed 302 Found in 3537ms
RegistrationsController :: authenticate_scope!
action : edit


Started GET "/users/edit" for 127.0.0.1 at 2011-06-08 11:42:09 -0500
  Processing by RegistrationsController#edit as HTML
  User Load (0.7ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 19 LIMIT 1
Completed   in 83ms
Warden::Strategies authenticate!
Warden::Strategies params: {"action"=>"new","controller"=>"sessions"}


Started GET "/users/sign_in" for 127.0.0.1 at 2011-06-08 11:42:10 -0500
  Processing by SessionsController#new as HTML
...

那么如何/我在哪里控制它被重定向到哪里?我正确地重设了“确认”属性?

解决方法

我们有一个类似的问题(主要是因为确认的用户在我们的系统中并不是一个被批准的用户),并决定使用user_status属性.它有2个状态 – “待定”,确认但尚未批准,并“已批准”.如果由于某些原因,该用户不再被批准(在您的情况下,他们更改了他们的电子邮件地址),那么我们将它们更改为待处理.

我们在applicationController上有一个before_filter,以根据他们的状态来验证他们应该去哪里.

def check_user_status
if current_user #logged in
 case current_user.status
   when "pending" 
     redirect_to root_path #user hasn't been approved yet
   when "approved"
     #tracking logic here   
  end
 end
end

希望这可以帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读