【问题标题】:Net::SMTPAuthenticationError in Devise::PasswordsController#create Username and Password not acceptedNet::SMTPAuthenticationError in Devise::PasswordsController#create 用户名和密码不被接受
【发布时间】:2018-09-09 01:34:53
【问题描述】:

自从我升级到 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" %>

开发.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 的访问权限。

我已经清除了验证码,启用了 POP/IMAP,在 gmail 中打开了不太安全的应用程序。它在开发和生产中都不起作用。正如我所提到的,我认为它甚至不会访问 gmail。我认为这是 Rails 5 和 Devise 4 之间的兼容性问题。当我升级时,注册和密码停止工作。我能够找到一个有效的示例 registrations_controller,但没有找到有关如何处理 passwords_controller 的任何文档。

【问题讨论】:

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


【解决方案1】:

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

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

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

【讨论】:

  • 看起来我的问题是 config/initializers/keys.rb 没有被加载,因为我在其他初始化程序中遇到了与 AWS 键相同的问题。当我取出引号(将它们用作应有的变量)时,由于“未初始化的常量”,我无法启动我的 Rails 服务器。
  • 另外,看起来初始化程序在启动服务器时按字母顺序运行,因为当我将 keys.rb 重命名为 aakeys.rb 时,我对其他初始化程序文件没有问题,但不幸的是初始化程序没有在环境之前运行文件。
猜你喜欢
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2016-07-05
  • 2020-02-05
  • 2022-01-01
  • 1970-01-01
  • 2020-01-19
  • 2019-06-10
相关资源
最近更新 更多