首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当用户更改电子邮件时重用activation_token和activation_digest

当用户更改电子邮件时重用activation_token和activation_digest
EN

Stack Overflow用户
提问于 2016-10-19 06:20:32
回答 1查看 58关注 0票数 0

我的用户模型是基于哈特尔的Rails教程。的,这里是用户模型的github页面

当用户更改他们的电子邮件地址,我希望他们回到帐户验证过程。可以重用attr_accessor: :activation_tokencolumn: activation_digest吗?

我尝试创建以下方法:

代码语言:javascript
运行
复制
user.rb
#the goal here is to reset activation_token and activation_digest
def deactivated
    update_attribute(:activated, false)
end

def create_activation_digest
    self.activation_token = User.new_token
    self.activation_digest = User.digest(activation_token)
end    

# These below are all pre-existing!

def send_activation_email
    UserMailer.account_activation(self).deliver_now
end


def User.digest(string)
  cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                BCrypt::Engine.cost
  BCrypt::Password.create(string, cost: cost)
end

def User.new_token
  SecureRandom.urlsafe_base64
end

def authenticated?(attribute, token)
    digest = send("#{attribute}_digest")
    return false if digest.nil?
    BCrypt::Password.new(digest).is_password?(token)
end

def activate
    update_attribute(:activated,    true)
    update_attribute(:activated_at, Time.zone.now)
end  

users_controller

# My Code
if !params[:user][:email].blank? 
  if @user.authenticate(params[:user][:current_password])
    params[:user].delete :current_password
    @user.update_attributes(email_user_params)
      if @user.save 
        @user.deactivated
        @user.create_activation_digest
        @user.send_activation_email
        log_out
        flash[:notice] = "Please check email pal"
        redirect_to root_url
      else 
        flash[:danger] = "Email Update Failed"
        redirect_to edit_user_email_path(@user)
      end
  else 
    flash[:danger] = "Password entered was incorrect"
    redirect_to edit_user_email_path(@user)
  end  
end  

#pre-existing 
class AccountActivationsController < ApplicationController
  def edit
    user = User.find_by(email: params[:email])
    if user && !user.activated? && user.authenticated?(:activation, params[:id])
      user.activate
      log_in user 
      flash[:success] = "Account activated!"
      redirect_to user
    else
      flash[:danger] = "Invalid activation link"
      redirect_to root_url
    end
  end
end    

这会发送一封激活电子邮件,它会更改activated属性,但是当单击链接时,它只会提供来自account_activations_controller的失败错误消息,而不会从heroku那里得到导致错误的线索。

这是邮递员

代码语言:javascript
运行
复制
Hi <%= @user.name %>,

<%= edit_account_activation_url(@user.activation_token, email: @user.email) %>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-19 21:55:28

这不起作用,因为我的方法被列为私有的。公开让上面的代码起作用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40123647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档