Ignore:
Timestamp:
09/24/26 17:43:19 (6 days ago)
Author:
Stefan <trsunovstefan@…>
Branches:
main
Children:
0cee8ec
Parents:
a531b45
Message:

Wiki docs, phase 6 and phase 7 added

File:
1 edited

Legend:

Unmodified
Added
Removed
  • docs/P4-Prototype/UseCase0001Implementation.md

    ra531b45 ref1c1c7  
    1 # Use-case 0001 Implementation — Register
     1# Use-case 0001 Implementation - Register new account
    22
    3 **Initiating actor:** Visitor. **Source file:** `server/auth.go`, function `Register`.
     3**Initiating actor:** Visitor
    44
    5 ## Scenario (implemented)
     5**Other actors:** —
    66
    7 1. **User** chooses option `[1] Register` from the anonymous menu.
     7A new person creates an account on EduBerza so that they can later log in as a
     8Trader ([UseCase0002](UseCase0002Implementation.md)). The Visitor enters a
     9username, an e-mail address, a full name and a password. The system validates the
     10input (required fields, an `@` in the e-mail, a password of at least 6 characters),
     11refuses a username or e-mail that is already registered, and stores only a SHA-256
     12hash of the password, never the password itself. A new account starts with a cash
     13balance of 0 USD; money is added later with a deposit
     14([UseCase0003](UseCase0003Implementation.md)).
    815
     16Original use-case description (P3): [UseCase0001](../P3-UseCaseModel/UseCase0001.md).
     17Implementation: [`server/auth.go`](../../server/auth.go), function `Register`
     18(the password hash is computed by `hashPassword` in the same file).
    919
    10 2. **System** prompts for username, email, full name and password (`server/auth.go:20-23`).
     20## Scenario
    1121
    12 3. **User** enters the values.
     221. **Visitor** chooses `[1] Register` in the anonymous menu (types `1`).
     232. **System** prints `-- Register --` and asks, one prompt after another, for
     24   `Username:`, `Email:`, `Full name:` and `Password (min 6 chars):`.
    1325
    14 4. **System** validates (empty checks, `@` in email, ≥ 6 chars in password) and then asks the database:
     26   The screenshot shows steps 1–2: option `1` is chosen and the first prompt
     27   (`Username:`) is waiting for input.
     28
     29   ![UC0001 steps 1-2: Visitor chooses Register, system asks for the data](screenshots/uc0001_1_register.png)
     30
     313. **Visitor** enters the values: `marko`, `marko@example.com`, `Marko Markovski`,
     32   `secret1`.
     334. **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.
     395. **System** checks whether the username or the e-mail already exists
     40   (`$1` = username, `$2` = e-mail):
    1541
    1642   ```sql
    17    SELECT EXISTS(
    18        SELECT 1 FROM users WHERE username = $1 OR email = $2
    19    );
     43   SELECT EXISTS(SELECT 1 FROM users WHERE username = $1 OR email = $2)
    2044   ```
    2145
    22 
    23 5. **System** inserts the new row (password hashed in Go, not in SQL, to keep hashing identical between register and login):
     46   If the result is `true`, alternate flow 5a applies.
     476. **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):
    2453
    2554   ```sql
    2655   INSERT INTO users (username, email, full_name, password_hash, available_balance)
    27    VALUES ($1, $2, $3, $4, 0);
     56   VALUES ($1, $2, $3, $4, 0)
    2857   ```
    2958
    30 6. **System** confirms: `Account created. You can now log in.`
     597. **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).
    3162
    32    ![Registering a new account](screenshots/uc0001_register.png)
     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   ![UC0001 steps 3-7: valid data, account created](screenshots/uc0001_3_7_created.png)
     68
     69All 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
     74In step 3 the Visitor entered `marko.example.com` (no `@`). The check in step 4
     75fails, the system prints `Invalid email.` and no SQL statement is executed. In the
     76prototype 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![UC0001 alternate flow 3a: invalid e-mail](screenshots/uc0001_3a_invalid_email.png)
     80
     81### Alternate flow 5a — duplicate username or e-mail
     82
     83After `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
     85query from step 5 (`$1` = `marko`, `$2` = `other@example.com`) returns `true`
     86because the username is taken, so the system prints
     87`Username or email already taken.`, does not run the `INSERT`, and the scenario
     88ends in the anonymous menu.
     89
     90![UC0001 alternate flow 5a: username already taken](screenshots/uc0001_5a_duplicate.png)
    3391
    3492## How to reproduce
    … …  
    3795./eduberza -init      # optional: reset to a known state
    3896./eduberza
    39 # choose [1] Register, then enter username / email / full name / password
     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.
    40100```
    41101
    42 The screenshot above is from an actual run: registering `marko` /
    43 `marko@example.com`, followed by the confirmation line.
     102All three screenshots come from one real run of exactly these inputs.
Note: See TracChangeset for help on using the changeset viewer.