2013-09-30 5 views
0

사용자 인증을 위해 Devise를 사용하는 레일 애플리케이션이 있습니다. 사용자가 등록 양식을 사용하여 등록했지만 오류가 발생하지 않고 유효한 유효한 사용자 이름/암호를 사용하는 경우 사용자로 로그인하기 만하면됩니다. 그러나 나는 그것을하는 방법을 이해할 수 없다.등록 컨트롤러 로그인 사용자와 Devise

제안 사항?

답변

0

devise RegistrationsController create 작업을 재정의해야합니다. 일반 등록 코드 다음에 코드를 추가하여 사용자가 이미 있는지 확인하고 전자 메일/패스 조합이 일치하는 경우 로그인해야합니다. 이런 식으로 뭔가 -

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 

(사용중인 유증의 버전에 따라이는 2.2.4의 소스를 기반으로) 그리고뿐만 아니라 당신의 경로에 컨트롤러를 추가

devise_for :users, :controllers => { :registrations => "registrations"}

관련 문제