# User Manual

## Password Reset CLI

This project includes a CLI command to reset user passwords directly from the backend workspace.

## Prerequisites

- Run commands from the backend folder:
  - `F:\git_ops\Pavti-Pustak\pp_test_backend`
- Ensure database connection settings in `config/db.js` are valid.
- Ensure the target user exists.
- Ensure `users.password` column is at least `VARCHAR(250)`.
  - One-time fix script: `database/test_db_users_fix_password_column.sql`

## Available Command

```bash
npm run reset:password -- --help
```

This prints usage help for the password reset tool.

## Reset Password by Username

```bash
npm run reset:password -- --username <username> --password <newPassword>
```

Example:

```bash
npm run reset:password -- --username amit_sharma --password NewPass@123
```

## Reset Password by User ID

```bash
npm run reset:password -- --id <userId> --password <newPassword>
```

Example:

```bash
npm run reset:password -- --id 8 --password NewPass@123
```

## Validation Rules

- Either `--username` or `--id` is required.
- `--password` is required.
- Password must be at least 8 characters.
- Password is stored as a bcrypt hash (not plain text).

## Success Output

On success, the script prints:

```text
Password updated for user <username> (id: <id>).
```

## Common Errors

- `User not found.`
  - Verify username or ID is correct.
- `Invalid --id. It must be a number.`
  - Pass a numeric value for `--id`.
- `Password must be at least 8 characters.`
  - Use a stronger password.
- `Password reset failed: ...`
  - Check DB connectivity and credentials in `config/db.js`.
- `Password reset failed: users.password column is too short for bcrypt hash.`
  - Run `database/test_db_users_fix_password_column.sql` and retry.

## Security Notes

- Avoid sharing shell history where passwords are visible.
- Prefer strong passwords with mixed case, numbers, and symbols.
- Rotate credentials after emergency resets.

## Related Files

- CLI script: `inscripts/reset-user-password.js`
- npm script definition: `package.json`
- Login route: `routes/authentication.js`
- Login rate limit middleware: `middleware/loginRateLimit.js`
