【问题标题】:how to skip email confirmation using devise_token_auth?如何使用 devise_token_auth 跳过电子邮件确认?
【发布时间】:2015-03-24 17:27:32
【问题描述】:

我正在为我的 Rails 应用程序使用 devise_token_auth gem。用户注册完成后,我想登录应用程序。如何跳过确认。

我试过config.reconfirmable = false,但没有成功

谁能建议我或帮助我如何实现这一目标? 我尝试了提供的解决方案

Why won't Devise allow unconfirmed users to login even when allow_unconfirmed_access_for is set? 但还是结束了

{"success":false,"errors":["A confirmation email was sent to your account at test123@gmail.com. You must follow the instructions in the email before your account can be activated"]}

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4.2 devise-confirmable


    【解决方案1】:

    在模型定义中添加如下回调:

    before_save -> { skip_confirmation! }
    

    并从包含的设计模块中删除:confirmable

      devise :database_authenticatable, :recoverable,
             :trackable, :validatable, :registerable,
             :omniauthable # there is no :confirmable here
    

    【讨论】:

    • 删除 :confirmable 迁移和 devise :confirmable 对我来说已经足够了。
    【解决方案2】:

    或者,如果您想跳过为某些人发送电子邮件确认但继续为其他人发送(例如基于电子邮件地址),您可以执行类似的操作。

    # app/models/user.rb
    after_create :skip_confirmation_email_for_some_user
    
    # ...
    
    protected
      def skip_confirmation_email_for_some_user
        if self.email.include? "noconfirm"
          self.skip_confirmation!
          self.confirm
        end
      end
    

    在 Rails 5.1.6 和 Devise Token Auth 0.2.0 上测试

    【讨论】:

      【解决方案3】:

      如果有人出现这个错误,include DeviseTokenAuth::Concerns::User 应该在devise :database_authenticable... https://github.com/lynndylanhurley/devise_token_auth/issues/397#issuecomment-166113011 之后

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-20
        • 1970-01-01
        • 2014-01-14
        • 2020-06-15
        相关资源
        最近更新 更多