Devise中的Net :: SMTPAuthenticationError :: PasswordsController#create不接受用户名和密码

时间:2018-03-30 01:11:54

标签: ruby-on-rails devise passwords gmail ruby-on-rails-5

自从我升级到Rails 5后,密码重置电子邮件不再有效。正如对所有相关问题的回复所示,我的gmail设置为接受来自不太安全的应用程序的登录,验证码已关闭

当我点击localhost上的发送重置密码说明时(也没有在生产中工作,但我先在本地调试),错误信息是

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)

路线:

  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

设计/口令/新:

<%= 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 => "me@gmail.com",
  :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

控制器/用户/ 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

这不是其他100个问题的重复,例如gmail不允许从应用程序登录。我曾经遇到过这个问题,但总会收到gmail的通知,说明外国应用正在访问gmail帐户。在这种情况下,我的应用似乎没有足够的距离来请求Gmail的访问权限。

我已经清除了CAPTCHA,启用了POP / IMAP,在gmail中启用了不太安全的应用程序。它不在开发和生产中工作。正如我所提到的,我不认为它甚至可以访问Gmail。我认为它与Rails 5和R之间的兼容性有关。设计4.当我升级,注册&amp;密码停止工作。我找到了一个有效的registrations_controller示例,但没有找到任何关于如何处理passwords_controller的文档。

1 个答案:

答案 0 :(得分:0)

这里发生了很多事情,我搞砸了很多东西,最终让它在生产和工作中发挥作用。发展。

'GMAIL_PWD'在development.rb中引用。 GMAIL_PWD在config / initializers / keys.rb中定义。当我使用实际密码而不是变量时,它可以工作。所以我把development.rb放在.gitignore中。

它现在也在生产中,但我不知道哪些解决方案有帮助。