如何将 devise_token_auth 与 Devise、Angular 和 Mongoid 一起使用

How to use devise_token_auth with Devise, Angular and Mongoid

我正在尝试使用 Mongoid、设计、devise_token_auth 和 ng-token-auth 为基于令牌的授权使用 Rails 编写的 API 以及 Mongoid 和 Angular作为客户。

问题是当我按照步骤安装 devise_token_auth 时,当我重新启动我的 Rails 应用程序时出现错误:undefined methodtable_exists?' User:Class`

我假设因为我使用的是 Mongoid,所以 User class 没有 table_exists? 方法。

我该如何解决这个问题?或者,更重要的是,我怎样才能让它发挥作用?

编辑:这是我的用户 class

class User

  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Enum

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  ## Database authenticatable
  field :email,              type: String, default: ""
  field :encrypted_password, type: String, default: ""

  ## Recoverable
  field :reset_password_token,   type: String
  field :reset_password_sent_at, type: Time

  ## Rememberable
  field :remember_created_at, type: Time

  ## Trackable
  field :sign_in_count,      type: Integer, default: 0
  field :current_sign_in_at, type: Time
  field :last_sign_in_at,    type: Time
  field :current_sign_in_ip, type: String
  field :last_sign_in_ip,    type: String

  ## Confirmable
  field :confirmation_token,   type: String
  field :confirmed_at,         type: Time
  field :confirmation_sent_at, type: Time
  field :unconfirmed_email,    type: String # Only if using reconfirmable

  include DeviseTokenAuth::Concerns::User

  attr_accessor :reset_token

  enum :role, [:admin, :author]

  after_initialize :set_default_role, :if => :new_record?
  before_create :set_auth_token

  field :first_name,                                        type: String
  field :last_name,                                         type: String
  field :domain,                                                type: String
  field :payment_details,                               type: Hash
  field :subscriber,                                        type: Boolean
  field :stripe_details,                                type: Hash
  field :theme,                                                 type: String

  # Validation
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
    before_save { self.email = email.downcase }
    before_create :create_remember_token


  # Get rid of devise-token_auth issues from activerecord
  def table_exists?
    true
  end

  def columns_hash
    # Just fake it for devise-token-auth; since this model is schema-less, this method is not really useful otherwise
    {} # An empty hash, so tokens_has_json_column_type will return false, which is probably what you want for Monogoid/BSON
  end

  def set_default_role
      self.role ||= :admin
  end

end

编辑 2:添加堆栈跟踪

http://pastebin.com/yMcr9mZ4

没有看到错误或源代码,我猜你的 User class 看起来像:

class User
  include Mongoid::Document

  # Maybe some devise options here
end

table_exists?columns_hash 等方法被 devise-token-auth 使用,因为它假定您的用户模型继承自 ActiveRecord。例如,参见 devise_token_auth/app/models/devise_token_auth/concerns/user.rb:

的第 87-94 行
  module ClassMethods
    protected


    def tokens_has_json_column_type?
      table_exists? && self.columns_hash['tokens'] && self.columns_hash['tokens'].type.in?([:json, :jsonb])
    end
  end

一种解决方案是用猴子修补你的胜利之路。 And/or 您可以在 User class:

上实现那些缺失的方法
class User
  # Get rid of devise-token_auth issues from activerecord
  def self.table_exists?
    true
  end

  def self.columns_hash
    # Just fake it for devise-token-auth; since this model is schema-less, this method is not really useful otherwise
    {} # An empty hash, so tokens_has_json_column_type will return false, which is probably what you want for Monogoid/BSON
  end

  def self.serialize(*args)

  end

  include DeviseTokenAuth::Concerns::User


  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Enum

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  ## Database authenticatable
  field :email,              type: String, default: ""
  field :encrypted_password, type: String, default: ""

  ## Recoverable
  field :reset_password_token,   type: String
  field :reset_password_sent_at, type: Time

  ## Rememberable
  field :remember_created_at, type: Time

  ## Trackable
  field :sign_in_count,      type: Integer, default: 0
  field :current_sign_in_at, type: Time
  field :last_sign_in_at,    type: Time
  field :current_sign_in_ip, type: String
  field :last_sign_in_ip,    type: String

  ## Confirmable
  field :confirmation_token,   type: String
  field :confirmed_at,         type: Time
  field :confirmation_sent_at, type: Time
  field :unconfirmed_email,    type: String # Only if using reconfirmable



  attr_accessor :reset_token

  enum :role, [:admin, :author]

  after_initialize :set_default_role, :if => :new_record?
  before_create :set_auth_token

  field :first_name,                                        type: String
  field :last_name,                                         type: String
  field :domain,                                                type: String
  field :payment_details,                               type: Hash
  field :subscriber,                                        type: Boolean
  field :stripe_details,                                type: Hash
  field :theme,                                                 type: String

  # Validation
  valid_email_regex = /\A[\w+\-.]+@[a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
  before_save { self.email = email.downcase }
  before_create :create_remember_token




  def set_default_role
      self.role ||= :admin
  end



end

我自己用过 devise-token-auth 而没有 ActiveRecord,我可以告诉你这是可能的。我没有使用 mount_devise_token_auth_for 做路由,而是实现了我自己的使用相同底层功能的控制器。查看 devise-token-auth 中的控制器,您会发现您可以遵循相同的流程,同时将 ActiveRecord 方法替换为 Mongoid 方法。祝你好运。

将其包含在您的 gemfile 中

gem 'rails',                      '~> 5.1.4'
gem 'mongoid',                    '~> 6.2', '>= 6.2.1'
gem 'devise_token_auth',          git: 'https://github.com/BunHouth/devise_token_auth.git', branch: 'mongoid'
gem 'mongoid-locker', '~> 0.3.4'

运行 bundle install 并按照 master 分支配置。它对我有用。