# RestRight administrator access

## Create the first administrator

From the project folder, open an interactive PowerShell or VS Code terminal and run:

```powershell
npm run create-admin
```

Enter the administrator name, username, and email when prompted. The password and confirmation are hidden while typing, must match, and must be at least 12 characters. The command creates the `admins` table if needed and stores a bcrypt password hash in the existing SQLite database. It never prints the password.

The default database is `data\restright.db`, the same file used by the RestRight app. To use a different database, set `RESTRIGHT_DB_PATH` in the terminal before running both the setup command and the app.

The CLI is local-only; there is no public administrator registration route. The table definition is in `sql\admin-schema.sql`, and the application also applies it automatically at startup.

## Run and sign in

Set a strong session secret in the same terminal, then start the app:

```powershell
$env:SESSION_SECRET = node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
npm start
```

Open `http://localhost:3003/admin/login` and sign in with the username or email and password you created. Local development can run without `SESSION_SECRET`, but sessions will be invalidated whenever the server restarts.

The protected dashboard at `/admin/dashboard` shows database totals. It checks the administrator record and role on each request. Logout is a CSRF-protected POST that destroys the session.

## Production

Set a persistent, high-entropy `SESSION_SECRET` through the hosting provider's secret/environment configuration. Set `NODE_ENV=production` and serve the app behind HTTPS; production session cookies are `Secure`, `HttpOnly`, and `SameSite=Strict`. Configure the trusted reverse proxy correctly before enabling `trust proxy` in a different deployment topology. Failed admin logins are rate-limited per IP and username/email for 15 minutes; for multi-instance hosting, replace the in-memory limiter and default Express session store with shared persistent stores.
