0
votes

Since I upgraded to Rails 5 that password reset email no longer works. As the responses to all related questions suggest, my gmail is set to accept login from less secure apps, captcha is turned off

When I click Send me reset password instructions on localhost (doesn't work in production either, but I'm debugging locally first), the error message is

Net::SMTPAuthenticationError in Users::PasswordsController#create 535-5.7.8 Username and Password not accepted.
    Extracted source (around line #19):

17 puts resource_class
18 puts resource_params
19 self.resource = resource_class.send_reset_password_instructions(resource_params)

Routes:

  devise_for :users, :skip => [:sessions, :passwords], controllers: {registrations: "users/registrations", passwords: "users/passwords"}      as :user do
    get 'login' => 'devise/sessions#new', :as => :new_user_session
    post 'login' => 'devise/sessions#create', :as => :user_session
    delete 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
    get "signup", :to => 'devise/registrations#new', :as => :new_user_signup
    post "signup", :to => 'devise/registrations#create', :as => :user_signup
    get "password", :to => 'devise/passwords#new', :as => :reset_password
    get "password", :to => 'devise/passwords#edit', :as => :edit_user_password
    match '/password' => 'users/passwords#create', as: :user_password, via: [:post]
    match '/password' => 'devise/passwords#update', via: [:put, :patch]
  end

Devise/passwords/new:

<%= form_for(resource, :as => resource_name, :url => user_password_path, :html => { :method => :post }) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :email %><br />
  <%= f.email_field :email, :autofocus => true, :size => 40 %></div>

  <div><%= f.submit "Send me reset password instructions" %></div>
<% end %>

<%= render "devise/shared/links" %>

development.rb:

config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
  :address => "smtp.gmail.com",
  :port => 587,
  :domain => "mail.google.com",
  :user_name => "[email protected]",
  :password => "GMAIL_PWD",
  :authentication => :plain,
  :enable_starttls_auto => true,
  :openssl_verify_mode => 'none'
}
config.action_mailer.default_url_options = {
  :host => 'localhost:3000', :protocol => 'http' 
}

user.rb:

  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable

controllers/users/passwords_controller.rb:

class Users::PasswordsController < Devise::PasswordsController
  include ApplicationHelper

  prepend_before_action :require_no_authentication

  def create

    puts resource_class
    puts resource_params
    self.resource = resource_class.send_reset_password_instructions(resource_params)
    yield resource if block_given?

    if successfully_sent?(resource)
      respond_with({}, location: after_sending_reset_password_instructions_path_for(resource_name))
    else
      respond_with(resource)
    end
  end
end

application_helper.rb:

module ApplicationHelper
  def resource_name
    :user
  end

  def devise_mapping
    @devise_mapping ||= Devise.mappings[:user]
  end

end

This is not a duplicate of the 100 other questions like it where gmail does not allow signin from apps. I used to have that problem, but would always get a notice from gmail that a foreign app was accessing gmail account. My app does not seem to be getting far enough to request access from Gmail in this case.

I already cleared CAPTCHA, enabled POP/IMAP, turned Less Secure Apps on in gmail. It's not working in development nor in production. As I mentioned, I don't think it's even accessing gmail. I think its an issue with the compatibility between Rails 5 & Devise 4. When I upgraded, registrations & passwords stopped working. I was able to find an example registrations_controller that worked, but have not found any documentation regarding what to do about the passwords_controller.

1
No, this is not a duplicate of the 100 other questions just like this that have all the same answers. I already cleared CAPTCHA, enabled POP/IMAP. It's not working in development nor in production. As I mentioned, I don't think it's even accessing the passwords_controller. I think its an issue with the compatibility between Rails 5 & Devise 4. When I upgraded, registrations & passwords stopped working. I was able to find an example registrations_controller that worked, but have not found any documentation regarding what to do about the passwords_controller.ThinQtv

1 Answers

0
votes

There are a lot of things going on here, I messed around a lot to finally get this working in production & development.

'GMAIL_PWD' is in quotes in development.rb. GMAIL_PWD is defined in config/initializers/keys.rb. It works when I use the actual password instead of the variable. So I put development.rb in .gitignore.

It works in production now also, though I don't know which solutions helped.