From d87cae00e0e209f000f5d59249158ec930726864 Mon Sep 17 00:00:00 2001
From: Midhun Suresh <midhunr@element.io>
Date: Mon, 17 Jan 2022 13:42:27 +0530
Subject: [PATCH] Do not send username to /register endpoint

Let the homeserver choose a username for us.
This would mean:
- our random string generator does not need to take the grammar for
  mxid into account.
- we will never have a scenario in which a username is reused by
  chance.
---
 src/deps.d.ts                           | 1 +
 src/viewmodels/AccountSetupViewModel.ts | 9 ++++-----
 2 files changed, 5 insertions(+), 5 deletions(-)
 create mode 100644 src/deps.d.ts

diff --git a/src/deps.d.ts b/src/deps.d.ts
new file mode 100644
index 0000000..6ecc3c0
--- /dev/null
+++ b/src/deps.d.ts
@@ -0,0 +1 @@
+declare module "hydrogen-view-sdk";
diff --git a/src/viewmodels/AccountSetupViewModel.ts b/src/viewmodels/AccountSetupViewModel.ts
index 5791a7d..098cb0d 100644
--- a/src/viewmodels/AccountSetupViewModel.ts
+++ b/src/viewmodels/AccountSetupViewModel.ts
@@ -9,7 +9,6 @@ export class AccountSetupViewModel extends ViewModel {
     private _client: Client;
     private _state: ObservableValue<string>;
     private _termsStage?: any;
-    private _username: string;
     private _password: string;
 
     constructor(options) {
@@ -21,9 +20,8 @@ export class AccountSetupViewModel extends ViewModel {
     }
 
     private async _startRegistration(): Promise<void> {
-        this._username = generateRandomString(7);
         this._password = generateRandomString(10);
-        let stage = await this._client.startRegistration(this._homeserver, this._username, this._password, "Chatterbox");
+        let stage = await this._client.startRegistration(this._homeserver, null, this._password, "Chatterbox");
         if (stage.type === "m.login.terms") {
             this._termsStage = stage;
             this.emitChange("termsStage");
@@ -32,10 +30,11 @@ export class AccountSetupViewModel extends ViewModel {
 
     async completeRegistration() {
         let stage = this._termsStage;
-        while (stage !== true) {
+        while (typeof stage !== "string") {
             stage = await stage.complete();
         }
-        await this.login(this._username, this._password);
+        // stage is username when registration is completed
+        await this.login(stage, this._password);
     }
 
     async login(username: string, password: string): Promise<void> {
-- 
GitLab