authenticate method

Future<bool> authenticate(
  1. String? userName,
  2. String? password, {
  3. Connection? connection,
  4. AuthenticationScheme? authScheme,
  5. String? authDb,
})

Implementation

Future<bool> authenticate(String? userName, String? password,
    {Connection? connection,
    AuthenticationScheme? authScheme,
    String? authDb}) async {
  var credential = UsernamePasswordCredential()
    ..username = userName
    ..password = password;

  (connection ?? masterConnection).serverConfig.userName ??= userName;
  (connection ?? masterConnection).serverConfig.password ??= password;

  if (authScheme != null) {
    _authenticationScheme = authScheme;
  }
  if (authDb != null) {
    authSourceDb = Db._authDb(authDb);
  }

  if (_authenticationScheme == null) {
    throw MongoDartError('Authentication scheme not specified');
  }
  var authenticator =
      Authenticator.create(_authenticationScheme!, this, credential);

  await authenticator.authenticate(connection ?? masterConnection);

  (connection ?? masterConnection).serverConfig.isAuthenticated = true;
  return true;
}