我尝试使用Mongoid,devise,devise_token_auth和ng-token-auth进行基于令牌的授权,该授权用于使用Mongoid和Angular作为客户端在Rails中编写的API。
问题是,当我按照安装devise_token_auth
的步骤操作时,当我重新启动Rails应用程序时出现错误:undefined method
table_exists?'对于用户:Class`
我假设因为我使用Mongoid而User
类没有使用table_exists?
方法。
我该如何解决这个问题?或者,更重要的是我如何才能使其发挥作用?
编辑:这是我的用户类
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:添加堆栈跟踪
答案 0 :(得分:3)
没有看到错误或源代码,我的猜测是你的User
类看起来像:
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
一个解决方案就是通过自己的方式获得胜利。和/或您可以在User
类上实现这些缺失的方法:
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
方法。祝你好运。
答案 1 :(得分:0)
将此内容包含在您的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
分支配置进行操作。它对我有用。