Module: OmniAuth::Identity::Models::Mongoid

Defined in:
lib/omniauth/identity/models/mongoid.rb

Overview

Note:

Mongoid is based on ActiveModel, so validations are enabled by default.

Mongoid is an ORM adapter for MongoDB:
https://github.com/mongodb/mongoid

This module provides OmniAuth Identity functionality for Mongoid models,
including secure password handling and authentication key management.

Examples:

Usage

class User
  include Mongoid::Document

  include OmniAuth::Identity::Models::Mongoid

  field :email, type: String
  field :password_digest, type: String
end

user = User.new(email: 'user@example.com', password: 'password')
user.save

# Authenticate a user
authenticated_user = User.locate(email: 'user@example.com')
authenticated_user.authenticate('password') # => user or false

Class Method Summary collapse

Class Method Details

.included(base) ⇒ void

This method returns an undefined value.

Called when this module is included in a model class.

This method extends the base class with OmniAuth Identity functionality,
including secure password support and authentication key validation.

Parameters:

  • base (Class)

    the model class including this module



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/omniauth/identity/models/mongoid.rb', line 40

def self.included(base)
  base.class_eval do
    include(::OmniAuth::Identity::Model)
    include(::OmniAuth::Identity::SecurePassword)

    # validations: true (default) incurs a dependency on ActiveModel, but Mongoid is ActiveModel based.
    has_secure_password

    # @!method self.auth_key=(key)
    # Sets the authentication key for the model and adds uniqueness validation.
    #
    # @param key [Symbol, String] the attribute to use as the authentication key
    # @return [void]
    # @example
    #   class User
    #     include OmniAuth::Identity::Models::Mongoid
    #     self.auth_key = :email
    #   end
    def self.auth_key=(key)
      super
      validates_uniqueness_of(key, case_sensitive: false)
    end

    # @!method self.locate(search_hash)
    # Finds a record by the given search criteria.
    #
    # @param search_hash [Hash] the attributes to search for
    # @return [Mongoid::Document, nil] the first matching record or nil
    # @example
    #   User.locate(email: 'user@example.com')
    def self.locate(search_hash)
      where(search_hash).first
    end
  end
end