5
votes

I am trying to implement a user authentication system similar to one that Medium has where user just put their email address while signing up & they get a confirmation link in the mail, after clicking that link they are redirected back to the website and then asked to fill password and other details. I am using Devise & have found 2 articles but none of them is working. Stackoverflow has similar questions posted but no good solution.

https://github.com/plataformatec/devise/wiki/How-To:-Email-only-sign-up
https://github.com/plataformatec/devise/wiki/How-To:-Override-confirmations-so-users-can-pick-their-own-passwords-as-part-of-confirmation-activation

Is something wrong with these articles ?

1

1 Answers

0
votes

In order to register a user with out a password the first thing that we have to do is make sure we have access to the devise views like this

rails generate devise:views

the view that I care for the most at this time is app/views/devise/registrations/new.html.erb *but you make sure to look at them all make sure they match your application *

you want to find and remove it. this should be lines 11 - 22 if everything is default

  <div class="field">
    <%= f.label :password %>
    <% if @minimum_password_length %>
    <em>(<%= @minimum_password_length %> characters minimum)</em>
    <% end %><br />
    <%= f.password_field :password, autocomplete: "off" %>
  </div>

  <div class="field">
    <%= f.label :password_confirmation %><br />
    <%= f.password_field :password_confirmation, autocomplete: "off" %>
  </div>

Now the form is going to submit to the controller with out a password. This will trigger an error Password can't be blank

so what I do at this point is just give it a password like this

1) copy the devise registrations_controller.rb into your application

#app/controllers/devise/registrations_controller.rb
class Devise::RegistrationsController < DeviseController
  ...

  def create
    build_resource(sign_up_params)

    resource.password = SecureRandom.hex(10) # sets the password

    resource.save
    yield resource if block_given?
    if resource.persisted?
      resource.send_reset_password_instructions # send them instructions how to reset password

      if resource.active_for_authentication?
        set_flash_message! :notice, :signed_up
        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}"
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      set_minimum_password_length
      respond_with resource
    end
  end
  ...
end

I hope that this helps you can find my code here