Devise - 用户编辑电子邮件后确认 [英] Devise - Confirming after the user edits email

查看:81
本文介绍了Devise - 用户编辑电子邮件后确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找出这个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
如果params [:user] [:email]!= resource.email
send_confirmation = true
end

如果resource.update_with_password(params [resource_name])
set_flash_message:notice,:是否更新if_navigational_format?
sign_in resource_name,resource,:bypass => true

如果send_confirmation
resource.update_attributes(:confirmation_at => nil,:confirmation_sent_at => nil)
resource.send_confirmation_instructions
end

respond_with资源,: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!
放置RegistrationsController :: authenticate_scope!
putaction:#{params [:action]}

super
end

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

  ... 
重定向到http:// localhost:3000 / users / edit
完成302找到3537ms
注册控制器:: authenticate_scope!
操作:编辑


在2011-06-08 11:42:09 -0500
启动GET/ users / edit为127.0.0.1
注册控制器#编辑为HTML
用户加载(0.7ms)SELECTusers。* FROMusersWHEREusers。id= 19 LIMIT 1
完成83ms
监狱长: :策略认证!
Warden :: Strategies params:{action=>new,controller=>sessions}


启动GET/ users / sign_in for 127.0.0.1 at 2011-06-08 11:42:10 -0500
处理SessionsController#new as HTML
...

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

解决方案

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



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

  def check_user_status 
如果current_user #logged在
case current_user.status
当pending
redirect_to root_path #user尚未被批准
当批准
#tracking逻辑在这里
结束
结束
结束

希望这有帮助。


I've been trying to figure this out for 2 days. I'm confirming user accounts with email confirmations (via Devise). I finally got all that working, but the whole point was to validate that a person owns the email they claim they own. Therefore, I need to have it confirm again whenever the user changes their email.

In order to do this, I've created registrations_controller and have over written the update method. Mostly based off what Devise has, but I check to see if I need to send confirmation based on the update.

# 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

My problem is I'm not sure where in the process to be able to change where it gets redirected to. I have a page that explains that "an email has been sent in order to confirm your email". But if I try to put it after send_confirmation_instructions when the user clicks "update account" then they are logged out (pushed to the login screen), then when they confirm the account through the email, then they are directed to the page I wanted to show them.

I have a custom Warden Strategy with some puts in it and I also over wrote the before filter that Devise puts in:

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

  super
end

So it looks like it is trying to authenticate the user. The log reads as follows:

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

So how/where do I control where it gets redirected to? Am I properly resetting the "confirmation" attributes?

解决方案

We had a similar issue (mainly because a confirmed user is not really an approved user in our system)- and decided to go with a user_status attribute. It has 2 statuses - "pending" which is confirmed but not yet approved, and "approved". If for some reason the user was no longer approved (in your case, they changed their email address), then we change them back to pending.

We have a before_filter on the applicationController to verify where they should be going based on their status.

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

Hope this helps.

这篇关于Devise - 用户编辑电子邮件后确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆