[英]Avoid sign-in after confirmation link click using devise gem?
我正在使用devise
寶石,點擊確認鏈接后,我想直接登錄。 目前要求再次登錄。
最近我在devise初始化文件中添加了以下內容:
config.allow_insecure_token_lookup = true
config.secret_key = 'a8d814803c0bcc735ce657adc77793459d00154cdd7532c13d3489600dc4e963f86e14beb593a32cbe9dbbe9197c9ce50a30102f363d90350052dc8d69930033'
有什么建議?
在以前的Devise版本中,用戶在確認后自動登錄。 這意味着任何可以訪問確認電子郵件的人都可以通過單擊鏈接登錄某人的帳戶。
在電子郵件重新確認工作流程中自動簽名用戶也可能有害。 想象一下,用戶決定更改他的電子郵件地址,並在這樣做時,他在新的電子郵件地址上輸入錯誤。 電子郵件將被發送到另一個地址,該地址隨着手中的令牌,將能夠登錄該帳戶。
如果用戶立即糾正電子郵件,則不會造成任何傷害。 但如果沒有,其他人可以登錄該帳戶,用戶不會知道它發生了。
因此, Devise 3.1
在確認后不再自動簽署用戶。 通過在config / initializers / devise.rb中設置以下內容,可以在升級后暫時恢復舊行為:
config.allow_insecure_sign_in_after_confirmation = true
此選項僅暫時可用於幫助遷移。
Devise中不再支持config.allow_insecure_sign_in_after_confirmation
標志。
雖然您應該了解在用戶確認帳戶時自動登錄用戶可能存在的安全問題( http://blog.plataformatec.com.br/2013/08/devise-3-1-now-with-more-secure -defaults / ),對於某些應用程序而言,在用戶體驗方面的好處可能值得安全權衡。
畢竟,安全風險是:a)用戶錯誤輸入他們的電子郵件,b)他們沒有立即糾正他們的錯誤,c)他們輸入的電子郵件對應於有效和有效的電子郵件,d)錯誤接收的人電子郵件會打開它並單擊鏈接。
如果這是您的應用程序可接受的風險配置文件,您可以覆蓋設計ConfirmationsController:
class ConfirmationsController < Devise::ConfirmationsController
def show
self.resource = resource_class.confirm_by_token(params[:confirmation_token])
yield resource if block_given?
if resource.errors.empty?
set_flash_message(:notice, :confirmed) if is_flashing_format?
sign_in(resource) # <= THIS LINE ADDED
respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, resource) }
else
respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render :new }
end
end
end
並在您的routes.rb
路由到它:
devise_for :users, controllers: { confirmations: 'confirmations' }
使用更新版本的Devise,您可以執行以下操作。
config / routes.rb :
devise_for :users, controllers: { confirmations: 'users/confirmations' }
app / controllers / users / confirmations_controller.rb :
class Users::ConfirmationsController < Devise::ConfirmationsController
def show
super do |resource|
sign_in(resource)
end
end
end
看看mb21的答案,應該是
def show
super do |resource|
if resource.confirmation_sent_at > DateTime.now-2.hours && resource.errors.empty?
sign_in(resource)
end
end
end
confirmation_sent_at是電子郵件發送給用戶的時間,而不是confirm_at,這是用戶點擊鏈接的時刻,當發生時,它始終在服務器上的2小時內...
我們希望用戶在用戶創建后2小時或更短時間點擊電子郵件中的鏈接時自動登錄。 根據@ Sjor的回答,我們選擇了:
class ConfirmationsController < Devise::ConfirmationsController
def show
super do |resource|
if resource.confirmed_at > DateTime.now-2.hours && resource.errors.empty?
sign_in(resource)
end
end
end
end
在這里你有如何解決它。
此代碼將允許用戶在確認后自動登錄,僅在第一次確認他/她的帳戶時。
class ConfirmationsController < Devise::ConfirmationsController
before_action :maybe_auto_sign_in, only: :show
protected
def after_confirmation_path_for(resource_name, resource)
sign_in(resource) if @auto_sign_in
super
end
private
# Automatically sign in the user that confirmed his/her email first time.
def maybe_auto_sign_in
@auto_sign_in =
User.first_time_confirming?(params[:confirmation_token])
end
end
class User < ActiveRecord::Base
def self.first_time_confirming?(confirmation_token)
confirmation_token &&
User.where(confirmation_token: confirmation_token, unconfirmed_email: nil)
.exists?
end
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.