sja
sja

Reputation: 347

Unable to create authentication in Mongoid

So I'm getting tired of this, as for some reason this bug seem to have been in existence for so many months, that it must obviously not be bothering a lot of people. Which I fail to see why, as I'm never able to create a basic user authentication system.

The error I get is:

@selector={"_id"=>{"$oid"=>BSON::ObjectId('5527d409536962695c000000')}}
Can't canonicalize query: BadValue unknown operator: $oid

this happens as a result of me trying to create a helper method called current_user. Which I do with the following code:

private
def current_user
 @current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user

how do I fix this insanely annoying bug?

Upvotes: 0

Views: 116

Answers (1)

njny
njny

Reputation: 569

This is caused by the JSON representation of an ObjectId. Here is the solution my team and I have been using.

Add this to concerns/zero_oid_fix.rb

module ZeroOidFix
  extend ActiveSupport::Concern

  module ClassMethods
    def serialize_from_session(key, salt)
      record = to_adapter.get((key[0]["$oid"] rescue nil))
      record if record && record.authenticatable_salt == salt
    end
  end
end

Then in User.rb add

   include ZeroOidFix

Make sure that line is below the devise line (if you're using it).

Upvotes: 1

Related Questions