I'm trying to use Mongoid, devise, devise_token_auth and ng-token-auth for an token based authorisation for an API written in Rails with Mongoid and Angular as the client.
The problem is when I follow the steps to install devise_token_auth
I get an error when I restart my Rails app: undefined method
table_exists?' for User:Class`
I'm assuming that because I'm using Mongoid the User
class don't have the table_exists?
method.
How can I get around this? Or, more importantly how can I get this to work?
EDIT: Here's my User 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
EDIT 2: Adding stack trace
Without seeing errors or source code, my guess is that your User
class looks something like:
class User
include Mongoid::Document
# Maybe some devise options here
end
Methods like table_exists?
and columns_hash
are used by devise-token-auth
because it's assumed that your user models inherit from ActiveRecord
. See, for example, lines 87-94 of devise_token_auth/app/models/devise_token_auth/concerns/user.rb
:
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
One solution is to monkey patch your way to victory. And/or you can implement those missing methods on your 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
Having used devise-token-auth
without ActiveRecord
myself, I can tell you it's possible. Rather than do routes with mount_devise_token_auth_for
, I implemented my own controllers that used the same underlying functions. Look at the controllers in devise-token-auth
and you'll see that you can follow the same flow, while replacing ActiveRecord
methods with Mongoid
methods. Good luck.
include this in your 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'
run bundle install
and follow the master
branch configuration. it works for me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.