2026-08-06 16:53:48 +02:00
# `@gabriel20xx/node-backend`
Shared Node.js backend infrastructure for services that use PostgreSQL:
- validated `DATABASE_URL` and pool settings;
2026-10-19 21:46:52 +00:00
- one pool owner with startup retry, transient retry for read-only queries, transactions, and shutdown;
- PBKDF2 password hashing (`verifyAndUpgradePasswordAsync` returns a current-scheme hash for a login to store) and secure session-token helpers;
2026-08-06 16:53:48 +02:00
- short-lived, one-time admin reset codes printed to the server console;
- common account/session persistence;
2026-11-21 00:23:35 +01:00
- transaction-locked ordered migrations;
- OmniLogger log shipping: `createLogShipperFromEnv` (off unless `OMNILOGGER_URL` and `OMNILOGGER_INGEST_TOKEN` are both set), `LogShipper` , and `mirrorConsole` .
2026-08-06 16:53:48 +02:00
The package does not depend on Express. Each service keeps its own routes,
cookies, roles, validation policy, and domain repositories. Existing
2026-11-01 21:48:05 +01:00
media-related code can be migrated into this package incrementally; keep the
2026-08-06 16:53:48 +02:00
legacy package as a compatibility layer until every consumer has moved.
Pass a service-specific `tableName` to `runMigrations` when multiple services
share one PostgreSQL database, so their migration histories cannot collide.
2026-09-23 04:56:42 +02:00
Database startup uses `DB_INIT_RETRY_INTERVAL_MS` (default `5000` , minimum
`250` ) and `DB_INIT_RETRY_MAX_ATTEMPTS` (default `0` , retry indefinitely).
2026-10-19 21:46:52 +00:00
Run `npm test` from this package or `npm run test --workspace=@gabriel20xx/node-backend` from `shared` ; `npm run lint` runs ESLint with the repository's config.
`AuthClient` 's `register` , `login` , `totpVerify` , `totpDisable` , `requestPasswordReset` and `confirmPasswordReset` take an optional trailing `{ clientIp }` , sent to AuthGuard as `X-Forwarded-For` so its per-address limits see the end user. Pass an address you have verified (for example the socket peer, or `X-Forwarded-For` only from a proxy you trust), never a raw request header. `AuthStore` refuses an empty password hash and a username containing `@` , refuses `setTotpSecret` while TOTP is enabled (error code `AUTH_TOTP_ALREADY_ENABLED` ), and makes usernames unique ignoring case when no clashing pair already exists.
2026-09-14 09:35:45 +02:00
## `authclient`/`coreconfig`: adopting AuthGuard instead of local auth
`AuthClient` (`src/authclient.js` ) trades a service's own local user table for
HTTP calls to a centralized AuthGuard instance, mirroring the Go/Python/Java
clients. `client.setBaseUrl(url)` retargets an existing client's AuthGuard
URL in place, so a client can be constructed with `new AuthClient('')`
(usable right away, failing closed until a real URL arrives) and be handed a
URL later, rather than needing one known up front.
`coreconfig` (`src/coreconfig.js` , the Node equivalent of
`shared/backend/coreconfig` and `shared_backend.coreconfig` ) is a small
`Store` that holds AuthGuard's/CarrierNode's current base URL(s) and updates
live whenever ProjectConfigurator pushes a fresh
`PUT /api/core-services/internal` - wire `store.onUpdate` to
`authClient.setBaseUrl` so a project never needs to hardcode `AUTHGUARD_URL` :
```js
2026-10-19 21:46:52 +00:00
import { AuthClient , CoreConfigStore , internalSecretMatches } from '@gabriel20xx/node-backend' ;
2026-09-14 09:35:45 +02:00
const authClient = new AuthClient ( '' );
const coreConfig = new CoreConfigStore ({ authGuardUrl : '' });
coreConfig . onUpdate (( info ) => authClient . setBaseUrl ( info . authGuardUrl ));
// in your own route for PUT /api/core-services/internal:
app . put ( '/api/core-services/internal' , ( req , res ) => {
2026-10-19 21:46:52 +00:00
if ( ! internalSecretMatches ( req . get ( 'X-Internal-Auth' ), process . env . SECRET )) {
2026-09-14 09:35:45 +02:00
return res . status ( 401 ). json ({ error : 'internal service authentication required' });
}
2026-10-19 21:46:52 +00:00
let info ;
try {
info = coreConfig . set ( req . body );
} catch ( error ) {
return res . status ( 400 ). json ({ error : error . message });
}
2026-09-14 09:35:45 +02:00
res . json ({ ok : true , info : info . toJSON () });
});
```