A self-hosted JWT authentication service built with NestJS and TypeORM. Supports local (email/password) login and OpenID Connect (OIDC) single sign-on.
- Node.js 20+
- A PostgreSQL database called "authentication"
-
Create a
.envfile containing the following variables:Variable Required Description DATABASE_HOSTYes PostgreSQL host DATABASE_PORTYes PostgreSQL port DATABASE_NAMEYes PostgreSQL database name DATABASE_USERNAMEYes PostgreSQL user DATABASE_PASSWORDYes PostgreSQL password JWT_ACCESS_SECRETYes Secret for signing access tokens JWT_REFRESH_SECRETYes Secret for signing refresh tokens SESSION_SECRETYes Secret for server-side session storage (required for OIDC) APP_URLYes Public base URL of this service (e.g. http://localhost:3000)CORS_ORIGINYes Allowed CORS origin (e.g. http://localhost:5173)JWT_ACCESS_EXPIRATIONNo Access token lifetime — duration string or seconds (default: 15m)JWT_REFRESH_EXPIRATIONNo Refresh token lifetime — duration string or seconds (default: 7d)PORTNo Port for the service (default: 3000)Duration strings use the
msformat:15m,7d,1h, etc. Plain integers are treated as seconds.See
.env.examplefor a complete template. -
Install dependencies:
npm install
-
Run database migrations:
npm run migration:run
-
Start the service:
node dist/main
npm run start:devBuild and run with Docker:
docker build -t authentication .
docker run --env-file .env -p 3000:3000 authenticationOIDC is configured at runtime via the admin settings UI — no environment variables required. Once logged in as an ADMIN, navigate to Settings → Single Sign-On and provide:
- Issuer URL — the OIDC provider's discovery URL (e.g.
https://accounts.google.com) - Client ID and Client Secret — from your provider's app registration
- Callback URL — must be set to
{APP_URL}/login/oidc/callbackand registered with your provider
Saving valid OIDC credentials registers the strategy immediately — no restart needed. When OIDC is enabled, users who sign in via SSO are automatically created in the database on first login (if sign-up is enabled).
All responses follow the shape { success: boolean, value?: T, message?: string }. Tokens are delivered as httpOnly cookies (access_token, refresh_token).
| Method | Path | Auth | Description |
|---|---|---|---|
| POST | /login |
— | Local login (email + password). Sets token cookies. |
| POST | /sign-up |
— | Register a new user (if sign-up is enabled). |
| GET | /me |
JWT | Returns the currently authenticated user. |
| POST | /refresh |
Refresh token | Issues new token pair; rotates the refresh token. |
| POST | /refresh/invalidate |
JWT | Revokes the current refresh token and clears cookies. |
| GET | /login/oidc |
— | Initiates the OIDC authorization flow. |
| GET | /login/oidc/callback |
— | OIDC provider callback. Sets token cookies. |
| Method | Path | Auth | Description |
|---|---|---|---|
| GET | /config |
JWT, ADMIN | Returns full auth configuration (including OIDC secrets). |
| PATCH | /config |
JWT, ADMIN | Updates auth configuration. Reloads OIDC strategy if changed. |
| GET | /config/public |
— | Returns non-sensitive config: signupEnabled, oidcEnabled, oidcProviderName. |
All user endpoints require a valid JWT. Most require the ADMIN role.
| Method | Path | Auth | Description |
|---|---|---|---|
| GET | /users |
JWT, ADMIN | List all users. |
| POST | /users |
JWT, ADMIN | Create a user. |
| GET | /users/:id |
JWT | Get a user by ID. |
| PATCH | /users/:id |
JWT, ADMIN | Update a user's profile. |
| PATCH | /users/:id/role |
JWT, ADMIN | Update a user's role. |
| DELETE | /users |
JWT, ADMIN | Delete a user by email. |