source: docs/P4-Prototype/UseCase0001Implementation.md

main
Last change on this file was ef1c1c7, checked in by Stefan <trsunovstefan@…>, 5 days ago

Wiki docs, phase 6 and phase 7 added

  • Property mode set to 100644
File size: 4.7 KB
Line 
1# Use-case 0001 Implementation - Register new account
2
3**Initiating actor:** Visitor
4
5**Other actors:** —
6
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)).
15
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).
19
20## Scenario
21
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):`.
25
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):
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.
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):
53
54 ```sql
55 INSERT INTO users (username, email, full_name, password_hash, available_balance)
56 VALUES ($1, $2, $3, $4, 0)
57 ```
58
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).
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 ![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)
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
102All three screenshots come from one real run of exactly these inputs.
Note: See TracBrowser for help on using the repository browser.