diff --git a/lib/moped/authenticatable.rb b/lib/moped/authenticatable.rb index 88103b7..c53914f 100644 --- a/lib/moped/authenticatable.rb +++ b/lib/moped/authenticatable.rb @@ -11,22 +11,20 @@ module Authenticatable # @example Apply the authentication credentials. # node.apply_credentials({ "moped_test" => [ "user", "pass" ]}) # - # @param [ Hash ] credentials The authentication credentials in the form: + # @param [ Hash ] logins The authentication credentials in the form: # { database_name: [ user, password ]} # # @return [ Object ] The authenticated object. # # @since 2.0.0 def apply_credentials(logins) - unless credentials == logins - logouts = credentials.keys - logins.keys - logouts.each do |database| - logout(database) - end - logins.each do |database, (username, password)| - unless credentials[database] == [ username, password ] - login(database, username, password) - end + logouts = credentials.keys - logins.keys + logouts.each do |database| + logout(database) + end + logins.each do |database, (username, password)| + unless logged_in_databases[database] && credentials[database] == [ username, password ] + login(database, username, password) end end self @@ -44,6 +42,10 @@ def credentials @credentials ||= {} end + def logged_in_databases + @logged_in_databases ||= {} + end + # Login the user to the provided database with the supplied password. # # @example Login the user to the database. @@ -73,6 +75,7 @@ def login(database, username, password) raise Errors::AuthenticationFailure.new(authenticate, document) unless document["ok"] == 1 credentials[database] = [username, password] + logged_in_databases[database] = true end # Logout the user from the provided database. @@ -93,6 +96,7 @@ def logout(database) return end credentials.delete(database) + logged_in_databases.delete(database) end end end diff --git a/lib/moped/connection.rb b/lib/moped/connection.rb index 0041867..4b771cb 100644 --- a/lib/moped/connection.rb +++ b/lib/moped/connection.rb @@ -74,7 +74,7 @@ def connected? # # @since 1.0.0 def disconnect - credentials.clear + logged_in_databases.clear @sock.close rescue ensure