| 1 | # Use-case 0001 Implementation - Register new account
|
|---|
| 2 |
|
|---|
| 3 | **Initiating actor:** Visitor
|
|---|
| 4 |
|
|---|
| 5 | **Other actors:** —
|
|---|
| 6 |
|
|---|
| 7 | A new person creates an account on EduBerza so that they can later log in as a
|
|---|
| 8 | Trader ([UseCase0002](UseCase0002Implementation.md)). The Visitor enters a
|
|---|
| 9 | username, an e-mail address, a full name and a password. The system validates the
|
|---|
| 10 | input (required fields, an `@` in the e-mail, a password of at least 6 characters),
|
|---|
| 11 | refuses a username or e-mail that is already registered, and stores only a SHA-256
|
|---|
| 12 | hash of the password, never the password itself. A new account starts with a cash
|
|---|
| 13 | balance of 0 USD; money is added later with a deposit
|
|---|
| 14 | ([UseCase0003](UseCase0003Implementation.md)).
|
|---|
| 15 |
|
|---|
| 16 | Original use-case description (P3): [UseCase0001](../P3-UseCaseModel/UseCase0001.md).
|
|---|
| 17 | Implementation: [`server/auth.go`](../../server/auth.go), function `Register`
|
|---|
| 18 | (the password hash is computed by `hashPassword` in the same file).
|
|---|
| 19 |
|
|---|
| 20 | ## Scenario
|
|---|
| 21 |
|
|---|
| 22 | 1. **Visitor** chooses `[1] Register` in the anonymous menu (types `1`).
|
|---|
| 23 | 2. **System** prints `-- Register --` and asks, one prompt after another, for
|
|---|
| 24 | `Username:`, `Email:`, `Full name:` and `Password (min 6 chars):`.
|
|---|
| 25 |
|
|---|
| 26 | The screenshot shows steps 1–2: option `1` is chosen and the first prompt
|
|---|
| 27 | (`Username:`) is waiting for input.
|
|---|
| 28 |
|
|---|
| 29 | 
|
|---|
| 30 |
|
|---|
| 31 | 3. **Visitor** enters the values: `marko`, `marko@example.com`, `Marko Markovski`,
|
|---|
| 32 | `secret1`.
|
|---|
| 33 | 4. **System** validates the input in Go, without accessing the database:
|
|---|
| 34 | - username, e-mail and password must be non-empty, otherwise it prints
|
|---|
| 35 | `Username, email and password are required.` and the scenario ends;
|
|---|
| 36 | - the e-mail must contain `@` (see alternate flow 3a);
|
|---|
| 37 | - the password must be at least 6 characters long, otherwise it prints
|
|---|
| 38 | `Password must be at least 6 characters.` and the scenario ends.
|
|---|
| 39 | 5. **System** checks whether the username or the e-mail already exists
|
|---|
| 40 | (`$1` = username, `$2` = e-mail):
|
|---|
| 41 |
|
|---|
| 42 | ```sql
|
|---|
| 43 | SELECT EXISTS(SELECT 1 FROM users WHERE username = $1 OR email = $2)
|
|---|
| 44 | ```
|
|---|
| 45 |
|
|---|
| 46 | If the result is `true`, alternate flow 5a applies.
|
|---|
| 47 | 6. **System** creates the account (`$1` = username, `$2` = e-mail, `$3` = full name,
|
|---|
| 48 | `$4` = password hash). The hash is computed in Go by `hashPassword` as the
|
|---|
| 49 | hex-encoded SHA-256 of the password — the same value that P3's
|
|---|
| 50 | `encode(digest($4, 'sha256'), 'hex')` would produce in SQL. Hashing on the Go side
|
|---|
| 51 | keeps it identical to the check done at login (SQL as in the code, only the Go
|
|---|
| 52 | source indentation removed):
|
|---|
| 53 |
|
|---|
| 54 | ```sql
|
|---|
| 55 | INSERT INTO users (username, email, full_name, password_hash, available_balance)
|
|---|
| 56 | VALUES ($1, $2, $3, $4, 0)
|
|---|
| 57 | ```
|
|---|
| 58 |
|
|---|
| 59 | 7. **System** prints `Account created. You can now log in.` and returns to the
|
|---|
| 60 | anonymous menu; the Visitor can continue with
|
|---|
| 61 | [UseCase0002](UseCase0002Implementation.md).
|
|---|
| 62 |
|
|---|
| 63 | The screenshot shows steps 3–7 of the successful attempt (bottom half): the
|
|---|
| 64 | entered values, the confirmation and the anonymous menu again. The top half is the
|
|---|
| 65 | earlier rejected attempt from alternate flow 3a.
|
|---|
| 66 |
|
|---|
| 67 | 
|
|---|
| 68 |
|
|---|
| 69 | All statements are run on the `project` schema: the connection sets
|
|---|
| 70 | `search_path=project,public` (`server/db/db.go`), so `users` means `project.users`.
|
|---|
| 71 |
|
|---|
| 72 | ### Alternate flow 3a — invalid e-mail
|
|---|
| 73 |
|
|---|
| 74 | In step 3 the Visitor entered `marko.example.com` (no `@`). The check in step 4
|
|---|
| 75 | fails, the system prints `Invalid email.` and no SQL statement is executed. In the
|
|---|
| 76 | prototype the system then shows the anonymous menu again and the Visitor chooses
|
|---|
| 77 | `[1] Register` once more, which returns the scenario to step 2.
|
|---|
| 78 |
|
|---|
| 79 | 
|
|---|
| 80 |
|
|---|
| 81 | ### Alternate flow 5a — duplicate username or e-mail
|
|---|
| 82 |
|
|---|
| 83 | After `marko` has been created, the Visitor tries to register again with username
|
|---|
| 84 | `marko`, e-mail `other@example.com`, full name `Marko Two`, password `secret2`. The
|
|---|
| 85 | query from step 5 (`$1` = `marko`, `$2` = `other@example.com`) returns `true`
|
|---|
| 86 | because the username is taken, so the system prints
|
|---|
| 87 | `Username or email already taken.`, does not run the `INSERT`, and the scenario
|
|---|
| 88 | ends in the anonymous menu.
|
|---|
| 89 |
|
|---|
| 90 | 
|
|---|
| 91 |
|
|---|
| 92 | ## How to reproduce
|
|---|
| 93 |
|
|---|
| 94 | ```sh
|
|---|
| 95 | ./eduberza -init # optional: reset to a known state
|
|---|
| 96 | ./eduberza
|
|---|
| 97 | # [1] Register: marko / marko.example.com / Marko Markovski / secret1 -> Invalid email.
|
|---|
| 98 | # [1] Register: marko / marko@example.com / Marko Markovski / secret1 -> Account created.
|
|---|
| 99 | # [1] Register: marko / other@example.com / Marko Two / secret2 -> Username or email already taken.
|
|---|
| 100 | ```
|
|---|
| 101 |
|
|---|
| 102 | All three screenshots come from one real run of exactly these inputs.
|
|---|