Rails Devise prevent login immediately after Signu

2019-02-07 11:08发布

问题:

Use Case:

  1. Admin should be able to create user, and it should not attempt to login.
  2. Public User can crate account and after signup he should be redirected to sign-in page rather signing him in immediately.

Can somebody help me with this ?

Thanks :)

回答1:

What you need to do is to override Devise's RegistrationsController create method.

There is an excellent explanation for how to do it here.

This is Devise create action:

  # POST /resource
  def create
    build_resource

    if resource.save
      if resource.active_for_authentication?
        set_flash_message :notice, :signed_up if is_navigational_format?
        sign_in(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
      clean_up_passwords resource
      respond_with resource
    end
  end

After you override the controller, just remove sign_in(resource_name, resource)

You can also set the method after_sign_up_path_for(resource) to fit your needs