Repository: conan-io/conan

Issue:
[question] confusing log: conan tries to connect to remote anonymously
### What is your question?

Hi,

I'm slightly confused by output of conan during conan remote login call.

I'm doing it like this:
```
conan remote remove "*"
conan remote add my_remote_name https://my_remote_url
conan remote login -p my_token my_remote_name my_username
```

The output that I see from last command is:
```
Connecting to remote 'my_remote_name' anonymously
Authenticated in remote 'my_remote_name' with user 'my_username'
Changed user of remote 'my_remote_name' from 'None' (anonymous) to 'my_username' (authenticated)
```

If I do the same command again:
```
Connecting to remote 'my_remote_name' with user 'my_username'
Authenticated in remote 'my_remote_name' with user 'my_username'
Changed user of remote 'my_remote_name' from 'my_username' (authenticated) to 'my_username' (authenticated)
```

For me personally it's confusing to see first line "Connecting to remote 'my_remote_name' anonymously" when I'm providing token and username. Do I understand correctly that it just shows previous username here if it was present in local database? And actually there is no anonymous connection to server?

### Have you read the CONTRIBUTING guide?

- [x] I've read the CONTRIBUTING guide


Discussion:
Hi @Nekto89 

Thanks for the feedback.

The issue is that the message is taking the info from the DB, without waiting for later cli or other origin values. It might be better to try to print that message later, it will be more clear. Lets try to do it for the 2.16 (we are already closing 2.15)



Patch:
diff --git a/conans/client/rest/auth_manager.py b/conans/client/rest/auth_manager.py
index e79fd404070..bb1b4cf1f7b 100644
--- a/conans/client/rest/auth_manager.py
+++ b/conans/client/rest/auth_manager.py
@@ -23,13 +23,14 @@ class _RemoteCreds:
     def __init__(self, localdb):
         self._localdb = localdb
 
-    def get(self, remote):
+    def get(self, remote, msg=True):
         creds = getattr(remote, "_creds", None)
         if creds is None:
             user, token, _ = self._localdb.get_login(remote.url)
             creds = user, token
-            usermsg = f"with user '{user}'" if user else "anonymously"
-            ConanOutput().info(f"Connecting to remote '{remote.name}' {usermsg}")
+            if msg:
+                usermsg = f"with user '{user}'" if user else "anonymously"
+                ConanOutput().info(f"Connecting to remote '{remote.name}' {usermsg}")
             setattr(remote, "_creds", creds)
         return creds
 
@@ -49,7 +50,7 @@ def __init__(self, requester, cache_folder, localdb, global_conf):
 
     def call_rest_api_method(self, remote, method_name, *args, **kwargs):
         """Handles AuthenticationException and request user to input a user and a password"""
-        user, token = self._creds.get(remote)
+        user, token = self._creds.get(remote, msg=(method_name != "authenticate"))
         rest_client = RestApiClient(remote, token, self._requester, self._global_conf)
 
         if method_name == "authenticate":


Test patch:
diff --git a/test/integration/command/test_remote_users.py b/test/integration/command/test_remote_users.py
index 46b21945c3c..a1f4106a112 100644
--- a/test/integration/command/test_remote_users.py
+++ b/test/integration/command/test_remote_users.py
@@ -93,6 +93,7 @@ def test_command_user_with_password_spaces(self):
         servers = {"default": test_server}
         client = TestClient(servers=servers, inputs=["lasote", "mypass"])
         client.run(r'remote login default lasote -p="my \"password"')
+        assert "Connecting to remote" not in client.out
         assert "Changed user of remote 'default' from 'None' (anonymous) to 'lasote'" in client.out
         client.run('remote logout default')
         client.run(r'remote login default lasote -p "my \"password"')