0
我有一個使用Devise進行用戶認證的Rails應用程序。如果用戶使用註冊表單註冊,但他碰巧使用現有的有效用戶名/密碼,而不是提供錯誤,我只想登錄用戶。但我無法弄清楚如何做到這一點。註冊控制器如何使用Devise登錄用戶
有什麼建議嗎?
我有一個使用Devise進行用戶認證的Rails應用程序。如果用戶使用註冊表單註冊,但他碰巧使用現有的有效用戶名/密碼,而不是提供錯誤,我只想登錄用戶。但我無法弄清楚如何做到這一點。註冊控制器如何使用Devise登錄用戶
有什麼建議嗎?
您將需要重寫設計RegistrationsController創建操作。在常規註冊碼之後,您需要添加代碼以檢查用戶是否已經存在,並在電子郵件/密碼組合匹配時登錄。像這樣的東西(根據您所使用的色器件版本 - 這是基於2.2.4來源)
class RegistrationsController < Devise::RegistrationsController
def create
build_resource
if resource.save
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_navigational_format?
sign_up(resource_name, resource)
respond_with resource, :location => after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
expire_session_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
# Add code to check if user exists and sign in
# I am just pasting in the sign_in code here and you will need to customize it as per your needs
# You can directly use params and User model, or try an keep your code generic
self.resource = warden.authenticate!(auth_options)
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
respond_with resource, :location => after_sign_in_path_for(resource)
end
end
end
而且控制器添加到您的路線以及:
devise_for :users, :controllers => { :registrations => "registrations"}