我的 Rails 应用程序使用设计来处理注册、身份验证等。我正在使用可确认模块。错误是这样的——当用户使用电子邮件注册时,Devise 会发送两封带有不同确认链接的确认电子邮件。一个链接有效,另一个将用户引导至错误页面。
设计吐出与错误相关的消息:“确认令牌无效”并将用户带到重新发送确认电子邮件页面。
我正在使用 heroku 托管并使用 sendgrid 发送电子邮件。更新:该错误也发生在本地主机上。
我不知道这个错误的根源在哪里,这可能比你需要看到的代码更多:
models/user.rb
...
devise :database_authenticatable, :registerable, :omniauthable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :authentication_keys => [:login]
...
## callbacks
after_create :account_created
# called after the account is first created
def account_created
# check if this activiy has already been created
if !self.activities.where(:kind => "created_account").blank?
puts "WARNING: user ##{self.id} already has a created account activity!"
return
end
# update points
self.points += 50
self.save
# create activity
act = self.activities.new
act.kind = "created_account"
act.created_at = self.created_at
act.save
end
...
def confirmation_required?
super && (self.standard_account? || self.email_changed)
end
...
控制器/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def update
unless @user.last_sign_in_at.nil?
puts "--------------double checking whether password confirmation is required--"
## if the user has not signed in yet, we don't want to do this.
@user = User.find(current_user.id)
# uncomment if you want to require password for email change
email_changed = @user.email != params[:user][:email]
password_changed = !params[:user][:password].empty?
# uncomment if you want to require password for email change
# successfully_updated = if email_changed or password_changed
successfully_updated = if password_changed
params[:user].delete(:current_password) if params[:user][:current_password].blank?
@user.update_with_password(params[:user])
else
params[:user].delete(:current_password)
@user.update_without_password(params[:user])
end
if successfully_updated
# Sign in the user bypassing validation in case his password changed
sign_in @user, :bypass => true
if email_changed
flash[:blue] = "Your account has been updated! Check your email to confirm your new address. Until then, your email will remain unchanged."
else
flash[:blue] = "Account info has been updated!"
end
redirect_to edit_user_registration_path
else
render "edit"
end
end
end
end
控制器/omniauth_callbacks_controller
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
skip_before_filter :verify_authenticity_token
def facebook
user = User.from_omniauth(request.env["omniauth.auth"])
if user.persisted?
flash.notice = "Signed in!"
# if the oauth_token is expired or nil, update it...
if (DateTime.now > (user.oauth_expires_at || 99.years.ago) )
user.update_oauth_token(request.env["omniauth.auth"])
end
sign_in_and_redirect user
else
session["devise.user_attributes"] = user.attributes
redirect_to new_user_registration_url
end
end
end
配置/路由.rb
...
devise_for :users, controllers: {omniauth_callbacks: "omniauth_callbacks",
:registrations => "registrations"}
...
如果需要,我很乐意提供更多信息。我也愿意自定义/覆盖设计邮件行为,但我不知道该怎么做。
非常感谢!