<commit_before># User registration

In this assignment, you'll add the necessary code to allow users to register accounts with the system.

## Migrations

Translate the following entity relationship diagram into a Knex migration file.

```text
┌──────────────────────────────────────────────────────────────────┐
│                              users                               │
├────────────────┬─────────────────────────┬───────────────────────┤
│id              │serial                   │primary key            │
│first_name      │varchar(255)             │not null default ''    │
│last_name       │varchar(255)             │not null default ''    │
│email           │varchar(255)             │not null               │
|hashed_password |char(60)                 │not null               │
│created_at      │timestamp with time zone │not null default now() │
│updated_at      │timestamp with time zone │not null default now() │
└────────────────┴─────────────────────────┴───────────────────────┘
```

More specifically, the migration file should:

- Live in the `migrations` directory.
- Migrate one table per migration file.
- Pass all the tests when `npm test test/part3.migrations.test.js` is run.

## Routes

Next, update your server to handle the following HTTP request and send the appropriate HTTP response.

**NOTE:** The information in just the request body uses the `application/json` content type.

| Request Method | Request URL        | Request Body                                                                                                        | Response Status | Response Content-Type | Response Body |
|----------------|--------------------|---------------------------------------------------------------------------------------------------------------------|-----------------|-----------------------|---------------|
| `POST`         | `/users`           | `{ "first_name": "John", "last_name": "Siracusa", "email": "<EMAIL>", "password": "<PASSWORD>" }` | `200`           | `text/plain`          | `OK`          |

In the `routes/users.js` module, add the necessary middleware to handle above RESTful route table. Make sure the route handler securely registers new users using the following techniques.

- Only store a cryptographic hash of the password in the database.
- Do _not_ send the new user's password or hashed password in the response body.

You can run the following test suite to verify the positive case when the middleware responds with a `200` status code.

```shell
npm test test/part3.routes.users.test.js
```<commit_msg>Improve part3 instructions<commit_after># User registration

In this assignment, you'll add the necessary code to allow users to register accounts with the system.

## Migrations

Translate the following entity relationship diagram into a Knex migration file.

```text
┌──────────────────────────────────────────────────────────────────┐
│                              users                               │
├────────────────┬─────────────────────────┬───────────────────────┤
│id              │serial                   │primary key            │
│first_name      │varchar(255)             │not null default ''    │
│last_name       │varchar(255)             │not null default ''    │
│email           │varchar(255)             │not null               │
|hashed_password |char(60)                 │not null               │
│created_at      │timestamp with time zone │not null default now() │
│updated_at      │timestamp with time zone │not null default now() │
└────────────────┴─────────────────────────┴───────────────────────┘
```

More specifically, the migration file should:

- Live in the `migrations` directory.
- Migrate one table per migration file.
- Pass all the tests when `npm test test/part3.migrations.test.js` is run.

## Routes

Next, update your server to handle the following HTTP request and send the appropriate HTTP response.

**NOTE:** The information in just the request body uses the `application/json` content type.

| Request Method | Request URL        | Request Body                                                                                                        | Response Status | Response Content-Type | Response Body |
|----------------|--------------------|---------------------------------------------------------------------------------------------------------------------|-----------------|-----------------------|---------------|
| `POST`         | `/users`           | `{ "first_name": "John", "last_name": "Siracusa", "email": "<EMAIL>", "password": "<PASSWORD>" }` | `200`           | `text/plain`          | `OK`          |

In the `routes/users.js` module, add the necessary middleware to handle above RESTful route table. Make sure the route handler securely registers new users using the following techniques.

- Only store a cryptographic hash of the password in the database.
- Do _not_ send the new user's password or hashed password in the response body.

You can run the following test suite to verify the positive case when the middleware responds with a `200` status code.

```shell
npm test test/part3.routes.users.test.js
```

## Bonus

Next, update your server to handle the following problem HTTP requests and send the appropriate HTTP response.

**NOTE:** The information in just the request body uses the `application/json` content type.

| Request Method | Request URL        | Request Body                                                                                                        | Response Status | Response Content-Type | Response Body                |
|----------------|--------------------|---------------------------------------------------------------------------------------------------------------------|-----------------|-----------------------|------------------------------|
| `POST`         | `/users`           | `{ "first_name": "John", "last_name": "Siracusa", "password": "<PASSWORD>" }`                                     | `400`           | `text/plain`          | `Email must not be blank`    |
| `POST`         | `/users`           | `{ "first_name": "John", "last_name": "Siracusa", "email": "<EMAIL>" }`                             | `400`           | `text/plain`          | `Password must not be blank` |
| `POST`         | `/users`           | `{ "first_name": "John", "last_name": "Siracusa", "email": "<EMAIL>", "password": "<PASSWORD>" }` | `400`           | `text/plain`          | `Email already exists`       |

In the `routes/users.js` module, update the necessary middleware to handle above RESTful route table. Make sure the route handler continues to securely register new users as before.

You can run the following test suite to verify the negative cases when the middleware responds with a `400` status code.

```shell
npm test test/part3.routes.users.test.js
```
{'fraction_non_alphanumeric': 0.23259886181234496, 'fraction_numerical': 0.007587917700277251, 'mean_word_length': 1.6994879873966129, 'pattern_counts': {'":': 18, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2320525', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1581, 'n_words': 632}