Repository: rotki/rotki

Issue:
User already exists during setup (DAppNode)
## Problem Definition

Using DAppNode Package:

1. Install Rotki
2. Setup Rotki using Premium API Key with restore sync'd database option
3. Enter incorrect password
4. Get error that password could not decrypt database
5. Go back
6. Enter correct password
7. "User $USERNAME already exists. User data dir: /data/$USERNAME"

Can't proceed...have to delete and re-create Rotki package

## Logs

None

### System Description

Operating system: DAppNode
Rotki version: DAppNode Package `0.1.9 (v1.23.1 upstream)`



Discussion:
Thank you for the report Chase. Let me look at this.
I can confirm this happening and it's not only in docker. Will provide a fix in next release.

Resolving patch:
diff --git a/rotkehlchen/premium/sync.py b/rotkehlchen/premium/sync.py
--- a/rotkehlchen/premium/sync.py
+++ b/rotkehlchen/premium/sync.py
@@ -235,6 +235,23 @@ def _sync_if_allowed(
             log.info('User approved data sync from server')
             self._sync_data_from_server_and_replace_local()  # this may raise due to password
 
+    def _abort_new_syncing_premium_user(
+            self,
+            username: str,
+            original_exception: PremiumAuthenticationError,
+    ) -> None:
+        """At this point we are at a new user trying to create an account with
+        premium API keys and we failed. But a directory was created. Remove it.
+        But create a backup of it in case something went really wrong
+        and the directory contained data we did not want to lose"""
+        shutil.move(
+            self.data.user_data_dir,  # type: ignore
+            self.data.data_directory / f'auto_backup_{username}_{ts_now()}',
+        )
+        raise PremiumAuthenticationError(
+            f'Could not verify keys for the new account. {str(original_exception)}',  # noqa: E501
+        ) from original_exception
+
     def try_premium_at_start(
             self,
             given_premium_credentials: Optional[PremiumCredentials],
@@ -259,19 +276,7 @@ def try_premium_at_start(
             try:
                 self.premium = premium_create_and_verify(given_premium_credentials)
             except PremiumAuthenticationError as e:
-                log.error('Given API key is invalid')
-                # At this point we are at a new user trying to create an account with
-                # premium API keys and we failed. But a directory was created. Remove it.
-                # But create a backup of it in case something went really wrong
-                # and the directory contained data we did not want to lose
-                shutil.move(
-                    self.data.user_data_dir,  # type: ignore
-                    self.data.data_directory / f'auto_backup_{username}_{ts_now()}',
-                )
-                raise PremiumAuthenticationError(
-                    'Could not verify keys for the new account. '
-                    '{}'.format(str(e)),
-                ) from e
+                self._abort_new_syncing_premium_user(username=username, original_exception=e)
 
         # else, if we got premium data in the DB initialize it and try to sync with the server
         db_credentials = self.data.db.get_rotkehlchen_premium()
@@ -295,7 +300,11 @@ def try_premium_at_start(
             # if this is a new account, make sure the api keys are properly stored
             # in the DB
             if sync_database:
-                self._sync_if_allowed(sync_approval, result)
+                try:
+                    self._sync_if_allowed(sync_approval, result)
+                except PremiumAuthenticationError as e:
+                    self._abort_new_syncing_premium_user(username=username, original_exception=e)
+
             self.data.db.set_rotkehlchen_premium(self.premium.credentials)
         else:
             self._sync_if_allowed(sync_approval, result)


Test patch: