Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I realize this is late, but this might be helpful to others. I had a similar requirement as you. Basically, I wanted to have a user enter an email address, and persist the user as a guest. Once the user is promoted to a 'regular' user, I re-enable password authentication. First, I created a warden strategy based on the <a href="https://github.com/rurounijones/devise-gullible" rel="nofollow">devise-gullible</a> gem. I made a modification to the <code>authenticate</code> method:</p> <pre><code>class Guest &lt; Authenticatable def authenticate! resource = mapping.to.find_for_database_authentication(authentication_hash) if resource if resource.respond_to?(:guest?) and resource.guest? success!(resource) else fail(:regular_user) end else fail(:invalid) end end end </code></pre> <p>I define a guest user in my user model as follows:</p> <pre><code>def guest? self.roles.length == 0 end </code></pre> <p>I'm using CanCan with devise and a HABTM to handle the roles. Currently, I have a 'regular' user and an 'admin' user. A user is a guest if he has no assigned roles yet.</p> <p>Then, you need to override the devise registrations controller:</p> <pre><code>class Users::RegistrationsController &lt; Devise::RegistrationsController prepend_before_filter :allow_params_authentication!, :only =&gt; :create def create resource = warden.authenticate(:guest, { scope: resource_name, recall: "#{controller_path}#new" }) if resource set_flash_message(:notice, :signed_in) if is_navigational_format? sign_in(resource_name, resource) respond_with resource, :location =&gt; after_sign_in_path_for(resource) elsif warden.winning_strategy.message == :regular_user set_flash_message :notice, :regular_user if is_navigational_format? redirect_to new_session_path(resource_name) else super end end end </code></pre> <p>Note that the idea is that we attempt to authenticate a user running the guest strategy only. If he is already registered as a guest, just sign him in normally. If the guest strategy fails because he is registered, but is now a regular user, redirect to the normal sign in page.</p> <p>Now it is possible to persist some limited information I may wish to collect from a guest, and not require him to have to do a full sign up. I use a normal User model:</p> <pre><code>class User &lt; ActiveRecord::Base devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, :trackable, :omniauthable, :token_authenticatable # Setup accessible (or protected) attributes for your model attr_accessible :email, :password, :password_confirmation, :remember_me has_and_belongs_to_many :roles def self.new_with_session(params, session) super.tap do |user| if user.password.blank? user.password = Devise.friendly_token[0,31] # Also, we don't need to do email confirmation in this case. # The user will have the guest role, and we'll do a confirmation # when we promote him to a 'regular' user. user.skip_confirmation! end end end def has_role?(role) !!self.roles.find_by_name(role.to_s.camelize) end end </code></pre> <p>Note that I auto-generate a password in <code>build_with_session</code>. You can send a auth token at the time of your choosing, and then require the user to set a new password at that time. You'll also want to then change his role so that he becomes a regular user (or do whatever it is you want to note he is no longer a guest).</p> <p>Here's my partial that appears on the front page:</p> <pre><code>&lt;%= simple_form_for(resource, as: resource_name, url: user_registration_path, defaults: { required: false }, html: { class: 'well' }, wrapper: :bootstrap, validate: true) do |f| %&gt; &lt;%= f.error_notification %&gt; &lt;fieldset&gt; &lt;legend&gt;New here? Let's get started!&lt;/legend&gt; &lt;%= f.input :email, placeholder: 'user@example.com', validate: { uniqueness: false } %&gt; &lt;%= f.button :submit, "Get my quote!", class: 'btn-primary' %&gt; &lt;/fieldset&gt; &lt;% end %&gt; </code></pre> <p>So, this form functions as both a guest registration and login form. The only think I don't really care for so far is the the registrations controller is handling an authentication, but I didn't see an immediate way around this.</p> <p>I implemented this strategy in my local lib directory. Be sure to set your load path appropriately, and add <code>require 'devise_guest'</code> to <code>config/initializers/devise.rb</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload