首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将devise_token_auth与设计、角度和蒙古相结合使用

如何将devise_token_auth与设计、角度和蒙古相结合使用
EN

Stack Overflow用户
提问于 2015-08-04 06:41:48
回答 2查看 1.4K关注 0票数 1

我正在尝试使用Mongoid、devise、devise_token_auth和ng令牌-auth作为一个基于令牌的授权,它是用Rails编写的,并以Mongoid和Rails作为客户端。

问题是,当我按照安装devise_token_auth的步骤运行时,重新启动Rails应用程序:undefined methodtable_exists?供用户使用:类

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

我怎么才能避开这一切?或者,更重要的是,我怎样才能让它发挥作用?

编辑:这是我的用户类

代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-04 08:55:20

在没有看到错误或源代码的情况下,我猜您的User类如下所示:

代码语言:javascript
复制
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行。

代码语言:javascript
复制
  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类上实现这些缺失的方法:

代码语言:javascript
复制
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方法。祝好运。

票数 3
EN

Stack Overflow用户

发布于 2017-10-08 13:38:45

将此包括在您的宝石文件中。

代码语言:javascript
复制
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分支配置。对我来说很管用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31802383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档