Ruby on rails 如何与Desive、Angular和Mongoid一起使用Desive_token_auth
我正在尝试使用Mongoid、design、design_-token_-auth和ng-token-auth对一个以Mongoid和Angular作为客户端的Rails编写的API进行基于令牌的授权 问题是,当我按照步骤安装Ruby on rails 如何与Desive、Angular和Mongoid一起使用Desive_token_auth,ruby-on-rails,angularjs,devise,Ruby On Rails,Angularjs,Devise,我正在尝试使用Mongoid、design、design_-token_-auth和ng-token-auth对一个以Mongoid和Angular作为客户端的Rails编写的API进行基于令牌的授权 问题是,当我按照步骤安装design\u token\u auth时,我在重新启动Rails应用程序时出错:undefined methodtable\u exists?:User:Class` 我假设因为我使用的是Mongoid,User类没有table\u exists?方法 我怎样才能避开这
design\u token\u auth
时,我在重新启动Rails应用程序时出错:undefined method
table\u exists?:User:Class`
我假设因为我使用的是Mongoid,User
类没有table\u 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:添加堆栈跟踪
在没有看到错误或源代码的情况下,我猜您的
用户类类似于:
class User
include Mongoid::Document
# Maybe some devise options here
end
design-token-auth
使用table\u exists?
和columns\u hash
等方法,因为假定您的用户模型继承自ActiveRecord
。例如,参见设计令牌认证/app/models/designe令牌认证/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
一个解决办法是用猴子修补你的胜利之路。和/或您可以在用户
类上实现缺少的方法:
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
我自己使用了designe-token-auth
而没有ActiveRecord
,我可以告诉你这是可能的。我没有为
使用mount\u designe\u token\u auth\u进行路由,而是实现了自己的控制器,使用了相同的底层功能。查看designe-token-auth
中的控制器,您将看到可以遵循相同的流程,同时将ActiveRecord
方法替换为Mongoid
方法。祝你好运。将此信息包含在你的GEM文件中
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安装
并遵循master
分支配置。这对我有用。谢谢!似乎在隧道的尽头有了曙光:)我已经添加了你解释过的方法,但仍然得到了错误。我正在用我的用户更新我的答案
类知道会发生什么吗?再次感谢你的帮助我根据你的例子更新了我的例子。在包含designe\u token\u auth关注点之前,您需要创建这些方法,因此我对它们进行了重新排序。我对Mongoid了解不多,但假设它可以序列化一个数组(在本例中为tokens
),那么覆盖serialize
方法就可以了。最后,您的正则表达式作为动态常量赋值会导致错误,因此我将其改为小写。我们在这里创建的所有3个方法都由designe\u token\u auth
使用,以确定它是否需要在SQL数据库中将令牌数组序列化为JSON。您需要验证令牌反序列化是否仍能像预期的那样为gem工作。我已经接受了答案,但老实说,我放弃了,我正在编写自己的身份验证和角度库。请将此包含在您的gem文件“`gem'rails'、'~>5.1.4'gem'mongoid'、'~>6.2','>=6.2.1'gem'设计\u令牌\u身份验证',git:'',分支:'mongoid'gem'mongoid locker','~>0.3.4'``运行捆绑包安装
,并遵循主分支配置。它对我有用。